0

I have 7 checkboxes which shows week days,Now what i want to do is to store the text of each check box in single string on basis of selected checkbox. What i am doing is by using if else condition but it is bit long.

Any help how i can minimize the code.

The code goes like this for every condition.

 if(mon.isChecked()&& tue.isChecked()){
       MON=mon.getText().toString();
       TUE=tue.getText().toString();
       text=MON+","+TUE;
   }
Vipul Singh
  • 393
  • 9
  • 26

2 Answers2

0

You should use a onCheckBoxClickedListener and use a switch statement on the id.

Marco Menardi
  • 481
  • 6
  • 20
0

add

android:onClick="onCheckboxClicked" to your all check boxes

and write your onCheckboxclicked() as follows

public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch(view.getId()) {
        case R.id.mon:
            if (checked)
                text = text + " mon";
            else
            break;
        case R.id.tue:
            if (checked)
                text = text + " tue";
            else
            break;
    }
}
Gaurav
  • 3,615
  • 2
  • 27
  • 50