1

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();
        }
    }
}
  • I'd use an int variable and set a binary value to it, representing the checkbox states (00, 01, 10, 11) - the checkboxe checked events are setting this variable's value. then use that variable in a switch case (to check for 0, 1, 2, 4 respectively). For your reference on bitmasks: http://stackoverflow.com/a/4549243/2649012 – Phantômaxx May 01 '14 at 10:43
  • Not sure I get what you mean here Mr. Bob Malooga, though I try. – everywhere anytime May 01 '14 at 10:55
  • Maybe you lack some basics of informatics? – Phantômaxx May 01 '14 at 10:57
  • ... which word? bitmask? binary? and, or, nand, nor operators? bit shifting? it's really the basics... and it also easy to deal with. – Phantômaxx May 01 '14 at 11:14
  • the term 'bitmask' never came up in Digital Computer Organization--with the rest I am familiar. – everywhere anytime May 01 '14 at 11:21
  • Oh, I see. It's a binary number you apply to another one to compare the 1's and the 0's. Just in case your teachers forgot to mention this particular concept (maybe they will in some future lessons), here's a reference you might find interesting: http://en.wikipedia.org/wiki/Mask_(computing). – Phantômaxx May 01 '14 at 11:23
  • Thank you for your time and your references as well sir. – everywhere anytime May 01 '14 at 11:27
  • My pleasure. I find the whole concept very interesting; especially when applied to multiple CheckBoxes. – Phantômaxx May 01 '14 at 11:29
  • By the way, what might cause only one layout change to be permitted in the above case? – everywhere anytime May 01 '14 at 11:37
  • Fragments are the preferred way. Anyway, here's an interesting solution: http://stackoverflow.com/a/17070318/2649012 – Phantômaxx May 01 '14 at 11:55
  • 1
    Coool you should mention that sir, because I seen this article a while ago: http://stackoverflow.com/questions/15388661/can-i-partially-hide-a-layout ...which identified the same thing so I wrapped the relevant portions into vertical LinearLayouts and selected visibility:GONE -- maybe beat you and user3455363 to the punch on that one ;) thank you so much for confirmation and all of your time!! Since your suggestions technically did not come in the form of an 'Answer' I should check the other one I suppose. I'm actually still working on this but It should be alll right, thanks again, God Bless! – everywhere anytime May 01 '14 at 12:39
  • It's not wasted time anyway. ;) – Phantômaxx May 01 '14 at 12:42
  • my knowledge of android is still limited sir so some suggestions may not quite fit yet ;) I have visited this site many times, although never posted a relevant question so thank you very much for the positive experience. – everywhere anytime May 01 '14 at 12:51
  • One last thing? Which one of the answers would you 'check' if you were me? – everywhere anytime May 01 '14 at 12:54
  • You're welcome, my friend. My knowledge of Android is still limited too. But I have a solid VB and VB net (and QuickBasic, Amos, etc) background. By solid I mean over 30 years. Experience takes time. ;) – Phantômaxx May 01 '14 at 12:54
  • I find the one by @user3455363 (+1 by me) more fitting. Fragments are the "modern" (and Google's preferred) way to do things today. – Phantômaxx May 01 '14 at 12:57
  • You are a good man sir, keep up the good work! – everywhere anytime May 01 '14 at 12:59
  • Thank you. This was worth the whole time. :) – Phantômaxx May 01 '14 at 13:00
  • ..and it seems my reputation is a little low(11/15) to +1 any suggestions or else all would be. – everywhere anytime May 01 '14 at 13:01
  • Your time is valuable and no sir, thank you. Good day! – everywhere anytime May 01 '14 at 13:04

3 Answers3

2

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

user3455363
  • 408
  • 1
  • 5
  • 13
  • Your welcome.If you want you can create set of required controls grouped in layout for each case in your app and set thier visiblility to GONE if you want to show one group you just change visibility of appropriate group to Visible. Remember that visiblity = GONE makes that controll is not rendered and doesn't take place in your layout – user3455363 May 01 '14 at 11:48
  • 1
    Coool you should mention that sir, because I seen this article a while ago: http://stackoverflow.com/questions/15388661/can-i-partially-hide-a-layout ...which identified the same thing so I wrapped the relevant portions into vertical LinearLayouts and selected visibility:GONE -- maybe beat you and Mr. Bob Malooga to the punch on that one ;) thank you so much for confirmation and all of your time! Since his suggestions technically did not come in the form of an 'Answer' I should check this one I suppose. I'm actually still working on this but It should be alll right, thanks again, God Bless! – everywhere anytime May 01 '14 at 12:36
1

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.

Erfan
  • 163
  • 1
  • 8
  • I'm actually using EditText objects under the checkboxes in this case, so would that also work Erfan? – everywhere anytime May 01 '14 at 11:01
  • using TextView is the right way I guess, because you want to "show" info to user and not let him edit it. – Erfan May 01 '14 at 11:07
  • Erfan I'll actually be prompting the user for input to the program within these fields; sorry for the misunderstanding--my mistake and I revised my question. – everywhere anytime May 01 '14 at 11:16
  • ahah, then use EditText and you'll be fine, also using fragment is a good idea but it will give you a bit of problem if you want an easy solution this is yours :) – Erfan May 01 '14 at 11:50
  • I came back to let you guys know that I found some information which I feel would be most helpful given my limited knowledge of the subject.. here: http://stackoverflow.com/questions/15388661/can-i-partially-hide-a-layout ...BUT oh my goodness, I just realized that this may have been what you were saying all along but I misunderstood! THUMBS UP TO EVERYONE FOR THEIR HELP. Thank you again sir for your time and suggestions; God bless!! – everywhere anytime May 01 '14 at 12:46
0

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