-1

I got two CheckBoxes Let us consider it as ChckBx1 and ChckBx2.

Now I need to implement few methods call on checkBoxes checked or not.

For more clearance, if chckBx1 checked then call Method1, if ChckBx2 is checked call Method2 and if Both CheckBoxes are checked Call Method3.

There are three different conditions.

Please, provide few codes or examples.

Any help is appreciated.

Goblin シ
  • 90
  • 13

6 Answers6

1

First check if both are checked or not, if not return and check each state separetly:

if(chckBx2.isChecked() && chckBx1.isChecked()){
 //call method 3
  return;
}
if(chckBx2.isChecked()){
  //call method 2
  return;
}
if(chckBx1.isChecked()){
  //call method 1
  return;
}
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
1

You onCheckedChangeListener to subscribe to state change of your checkboxes. Compact and crisp example for the same could be

final CheckBox checkBox1, checkBox2;

        CompoundButton.OnCheckedChangeListener listener=new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(checkBox1.isChecked()&&checkBox2.isChecked())
                {
                    // do what you want to do if both are checked
                }
                else if(checkBox1.isChecked())
                {
                    // do what you want to do if only checkbox1 is checked
                }
                else if(checkBox2.isChecked())
                {
                    // do what you want to do if only checkbox2 is checked
                }
            }
        };

Initialize checkbox1&checkbox2 with their respective IDs Hope this would help your case

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
1

we need to use setOnCheckedChangeListener on checkBox.

ChckBx1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
         if (isChecked and ChckBx2.isChecked()) {
             method3();
          }

         if (isChecked) {
             method1();
         }
       }
   }
); 

and

    ChckBx2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

           @Override
           public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
             if (isChecked and ChckBx1.isChecked()) {
                 method3();
              }

             if (isChecked) {
                 method2();
             }
           }
       }
    ); 
Nagesh Jatagond
  • 344
  • 2
  • 13
0

simple you can check

if(checkBx1.isChecked() && checkBx2.isChecked()){
//call method three
 return;
} 
else if(checkBx1.isChecked()){
 //call method one
 return;
}
else if(checkBx2.isChecked()){
//call method two
 return;
}  
Rajesh Panchal
  • 1,140
  • 3
  • 20
  • 39
0

It is really simple just check the below code you will understand.

package com.example.checkbox;  

import android.os.Bundle;  
import android.app.Activity;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.*;  

public class MainActivity extends Activity {  
    CheckBox pizza,coffe,burger;  
    Button buttonOrder;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        addListenerOnButtonClick();  
    }  
public void addListenerOnButtonClick(){  
    //Getting instance of CheckBoxes and Button from the activty_main.xml file  
    pizza=(CheckBox)findViewById(R.id.checkBox1);  
    coffe=(CheckBox)findViewById(R.id.checkBox2);  
    burger=(CheckBox)findViewById(R.id.checkBox3);  
    buttonOrder=(Button)findViewById(R.id.button1);  

    //Applying the Listener on the Button click  
    buttonOrder.setOnClickListener(new OnClickListener(){  

        @Override  
        public void onClick(View view) {  
            int totalamount=0;  
            StringBuilder result=new StringBuilder();  
            result.append("Selected Items:");  
            if(pizza.isChecked()){  
                result.append("\nPizza 100Rs");  
                totalamount+=100;  
            }  
            if(coffe.isChecked()){  
                result.append("\nCoffe 50Rs");  
                totalamount+=50;  
            }  
            if(burger.isChecked()){  
                result.append("\nBurger 120Rs");  
                totalamount+=120;  
            }  
            result.append("\nTotal: "+totalamount+"Rs");  
            //Displaying the message on the toast  
            Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();  
        }  

    });  
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Hiren Jungi
  • 854
  • 8
  • 20
0
void callMethods(Checkbox checkbox1, Checkbox checkbox2){
if(checkbox1.isChecked() && checkbox2.isChecked()){
   //call method three
} 
else if(checkbox1.isChecked()){
    //call method one
  }
else if(checkbox2.isChecked()){
   //call method two
 }
}
Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25