I am trying to design an app which contains a list of check boxes followed by radio group (contains 4 radio buttons each).
Concept is,until we select the particular checkbox its relevant radio group should be disabled.I tried all solutions provided at
How to disable a RadioGroup until checkbox is checked
Android: How do I enable/disable a Checkbox, depending on a Radio button being selected first
Android disable radio group in xml
http://developer.android.com/reference/android/widget/RadioGroup.html but none of them are working for me..the app is crashing out..
Here is my code:
public class Service extends Activity
{
private String op1=" ",op2=" ";
private CheckBox ck1;
private CheckBox ck2;
private RadioGroup rg1;
private RadioGroup rg2;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
ck1 = (CheckBox)findViewById(R.id.checkBox1);
ck2 = (CheckBox)findViewById(R.id.checkBox2);
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.radio1:
op1="Normal";
break;
case R.id.radio2:
op1="Extra";
break;
}
}
});
rg2.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch(checkedId)
{
case R.id.radio3:
op2="Normal";
break;
case R.id.radio4:
op2="Extra";
break;
}
}
});
}
public void onCheckboxClicked(View view)
{
ck1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton checkBox, boolean isChecked)
{
RadioGroup rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
for(int i = 0; i < rg1.getChildCount(); i++)
{
((RadioGroup)rg1.getChildAt(i)).setEnabled(isChecked);
}
}
});
for(int i = 0; i < rg1.getChildCount(); i++)
{
((RadioGroup)rg1.getChildAt(i)).setEnabled(false);
}
}
ck2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton checkBox, boolean isChecked)
{
RadioGroup rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
for(int i = 0; i < rg2.getChildCount(); i++)
{
((RadioGroup)rg2.getChildAt(i)).setEnabled(isChecked);
}
}
});
for(int i = 0; i < rg2.getChildCount(); i++)
{
((RadioGroup)rg2.getChildAt(i)).setEnabled(false);
}
}
}
.xml code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="@string/Cheese" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/Normal />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/Extra />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="@string/Ketchup" />
<RadioGroup
android:id="@+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2" >
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/Normal />
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/Extra />
</RadioGroup>
</LinearLayout>
And to erase the radiobutton if checkbox is deselected again
if(isChecked) //in switch-case
//task
else
radiogroup.clearCheck();