How Can I Put Radiobuttons As Grandchildren From Their Radiogroup A Good Way?
Solution 1:
I will suggest you not to increase View Hierarchy by using nested Nested Layout (Linear/Relative) inside a RadioGroup. Also you will not get single selection feature using Nested Layout. RadioGroup actually extends LinearLayout. So it has only the capability of arranging RadioButtons either Vertically or Horizontally. Here I shared the link RelativeRadioGroup of my Library which is actually a RelativeRadioGroup so that you can arrange RadioButtons as you wish and specially selection will not be affected.
Solution 2:
As you are using LinearLayout
inside RadioGroup
, RadioGroup
loses its basic functionality
to toggle
button states
and also its override method onCheckedChanged()
never be called.
To solve this problem, you have to change the RadioButton
states programmatically
.
Here is a complete solution:
publicclassRadioGroupActivityextendsAppCompatActivityimplementsRadioButton.OnCheckedChangeListener {
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
RadioButton radioButton4;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_group);
getSupportActionBar().setTitle("Hello Android");
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.button1);
radioButton2 = (RadioButton) findViewById(R.id.button2);
radioButton3 = (RadioButton) findViewById(R.id.button3);
radioButton4 = (RadioButton) findViewById(R.id.button4);
radioButton1.setOnCheckedChangeListener(this);
radioButton2.setOnCheckedChangeListener(this);
radioButton3.setOnCheckedChangeListener(this);
radioButton4.setOnCheckedChangeListener(this);
}
@OverridepublicvoidonCheckedChanged(CompoundButton compoundButton, boolean status) {
if (status)
{
switch (compoundButton.getId()) {
case R.id.button1:
radioButton2.setChecked(false); radioButton3.setChecked(false); radioButton4.setChecked(false);
// Do somethingToast.makeText(getApplicationContext(), radioButton1.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
radioButton1.setChecked(false); radioButton3.setChecked(false); radioButton4.setChecked(false);
// Do somethingToast.makeText(getApplicationContext(), radioButton2.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton4.setChecked(false);
// Do somethingToast.makeText(getApplicationContext(), radioButton3.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button4:
radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton3.setChecked(false);
// Do somethingToast.makeText(getApplicationContext(), radioButton4.getText() + " clicked", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
OUTPUT:
Hope this will help~
Solution 3:
You can't put any other layout in RadioGroup. So if you still want to put all your RadioButtons in Linear layout, You'll have to do little extra work.
Do it programmatically, When any RadioButton is checked set others unchekced.
Something like
RadioButtonrbu1=(RadioButton)findViewById(R.id.radio0);
RadioButtonrbu2=(RadioButton)findViewById(R.id.radio1);
rbu1.setOnClickListener(first_radio_listener);
rbu2.setOnClickListener(second_radio_listener);
OnClickListenerfirst_radio_listener=newOnClickListener (){
publicvoidonClick(View v) {
rbu2.setChecked(false);
}
};
OnClickListenersecond_radio_listener=newOnClickListener (){
publicvoidonClick(View v) {
rbu1.setChecked(false);
}
};
Post a Comment for "How Can I Put Radiobuttons As Grandchildren From Their Radiogroup A Good Way?"