checkbox not working properly in android

I have made a simple checkbo[x demo program in android,in that i've put a chekbox and a button.I want is that when chekbox is pressed and button is pressed thet toast showing "i accept" else when button pressed toast generated as "i dont accept"...i have made it but problem is when i unchecked the chekbox..it shows toast directly(without clicking button);

mainActivity.java

package com.example.chechboxdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity {
CheckBox c1;
Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c1=(CheckBox)findViewById(R.id.checkBox1);
        b=(Button)findViewById(R.id.button1);
        c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // TODO Auto-generated method stub

                if(c1.isChecked()){

                    b.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub
                            Toast.makeText(getApplicationContext(), "i accept", 1).show();
                        }
                    });
                }
                else{
                    Toast.makeText(getApplicationContext(), "i don't accept", 1).show();

                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Answers


You shouldn't be using the checkChanged listener unless you are wanting something to happen when the user checks/unchecks the check box. Since you want the action to happen only when the button is clicked you need only your onClickListener and it should be outside of your onCheckChangedListener

Try like this:

public class MainActivity extends Activity {
CheckBox c1;
Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c1=(CheckBox)findViewById(R.id.checkBox1);
        b=(Button)findViewById(R.id.button1);
        c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                // You don't need to use onCheckChangeListener.
            }
        });

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(c1.isChecked()){
                    Toast.makeText(getApplicationContext(), "i accept", 1).show();
                }else{
                    Toast.makeText(getApplicationContext(), "i don't accept", 1).show();
                }
            }
        });
    }//end onCreate
}//end MainActivity

Need Your Help

Implementing RequestWrapper for RESTful WCF service

wcf rest wrapper webhttpbinding

I've written a simple object called RequestWrapper that contains single method of type:

spring MVC: problems in binding an enum value to an hidden field

java spring-mvc enums hidden-field

I have issues in binding enum values in hidden fields with spring mvc.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.