Question

I would really like to set the layout depending on which combination of two checkboxes are selected. Since there are four possible states, I have four layouts to display items underneath the checkboxes, if selected. I have made this work using four classes, but there must be a more efficient way to do this.

Basically, I would like to have drop-down EditTexts for user input displayed under the checkboxes only if they are selected. If the setContentView statements are replaced with the commented ones, I can cycle through any combination of checkboxes, but as the code is, only one layout change is able to be made and I don't understand why. Please help with any suggestions.

**I realize the CompoundButton object is unused here.

public class First extends Activity implements OnCheckedChangeListener{
    CheckBox emailBox,smsBox;

    @Override public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);
        emailBox=(CheckBox)findViewById(R.id.checkBox_1);
        smsBox=(CheckBox)findViewById(R.id.checkBox_2);
        emailBox.setOnCheckedChangeListener(this);
        smsBox.setOnCheckedChangeListener(this);
    }
    public void onCheckedChanged(CompoundButton compound,boolean isChecked){
        if(!emailBox.isChecked()&&!smsBox.isChecked()){
            setContentView(R.layout.activity_1);
//            Toast.makeText(First.this,"None Checked",Toast.LENGTH_SHORT).show();
        }
        if(emailBox.isChecked()&&!smsBox.isChecked()){
            setContentView(R.layout.activity_2);
//            Toast.makeText(First.this,"Email Checked",Toast.LENGTH_SHORT).show();
        }
        if(smsBox.isChecked()&&!emailBox.isChecked()){
            setContentView(R.layout.activity_3);
//            Toast.makeText(First.this,"Sms Checked",Toast.LENGTH_SHORT).show();
        }
        if(emailBox.isChecked()&&smsBox.isChecked()){
            setContentView(R.layout.activity_4);
//            Toast.makeText(First.this,"Both Checked",Toast.LENGTH_SHORT).show();
        }
    }
}
Was it helpful?

Solution

More efficient way will be use fragments instead of changing layouts :)
OR
Group Controls in layouts and set thier visiblity to View.GONE then set visiblility for appropriate group to View.Visible

OTHER TIPS

To display a drop-down info box (like a text view) you can place e.g a lable which is empty(or rather you hide it) under these two check boxes and check for changes in checkboxes and then change the lable I mentioned earlier in runtime using java part of the code to display what u want. I hope I've got your point and this will help you.

I was able to solve this issue very simply by wrapping the relevant portions of the layout in a vertical LinearLayout and toggling between visibility='gone' and visibility='visible' as indicated in the code below; so I eventually thought I should come back here to share (if anyone has a simpler, more efficient method, perhaps let me know?)

public class MainActivity extends Activity implements OnCheckedChangeListener{
    CheckBox emailBox,smsBox;
    LinearLayout portion_1,portion_2,portion_3;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        portion_1=(LinearLayout)findViewById(R.id.hider_1);
        portion_2=(LinearLayout)findViewById(R.id.hider_2);
        portion_3=(LinearLayout)findViewById(R.id.hider_3);
        emailBox=(CheckBox)findViewById(R.id.checkBox_1);
        smsBox=(CheckBox)findViewById(R.id.checkBox_2);
        emailBox.setOnCheckedChangeListener(this);
        smsBox.setOnCheckedChangeListener(this);
    }
    public void onCheckedChanged(CompoundButton compound,boolean isChecked){
        if(!emailBox.isChecked()&&!smsBox.isChecked()){
            portion_1.setVisibility(View.GONE);
            portion_2.setVisibility(View.GONE);
            portion_3.setVisibility(View.GONE);
            emailBox.setChecked(false);
            smsBox.setChecked(false);
        }
        if(emailBox.isChecked()&&!smsBox.isChecked()){
            portion_1.setVisibility(View.VISIBLE);
            portion_2.setVisibility(View.GONE);
            portion_3.setVisibility(View.VISIBLE);
            emailBox.setChecked(true);
            smsBox.setChecked(false);
        }
        if(smsBox.isChecked()&&!emailBox.isChecked()){
            portion_1.setVisibility(View.GONE);
            portion_2.setVisibility(View.VISIBLE);
            portion_3.setVisibility(View.VISIBLE);
            emailBox.setChecked(false);
            smsBox.setChecked(true);
        }
        if(emailBox.isChecked()&&smsBox.isChecked()){
            portion_1.setVisibility(View.VISIBLE);
            portion_2.setVisibility(View.VISIBLE);
            portion_3.setVisibility(View.VISIBLE);
            emailBox.setChecked(true);
            smsBox.setChecked(true);
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top