jigar
jigar

Reputation: 1591

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;
    }

}

Upvotes: 0

Views: 3570

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

move c1.isChecked() inside Button Click to check if CheckBox is checked or not on Button click then show toast message according to CheckBox status. change your code as :

b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(c1.isChecked()){  //<<< check here if checkbox clicked or not
                    Toast.makeText(getApplicationContext(), 
                                             "i accept", 1).show();
                }else{
                    Toast.makeText(getApplicationContext(), 
                                       "i don't accept", 1).show();
                }
            }
        });

Upvotes: 0

FoamyGuy
FoamyGuy

Reputation: 46856

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

Upvotes: 1

Related Questions