In the XML file:
<!--...-->
<RadioGroup
android:checkedButton="@+id/radioButton1"
android:id="@+id/radioGroupBase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:id="@+id/linearLayout1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:clickable="false"
android:focusable="false"
android:id="@+id/radioButton1"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
<TextView
android:clickable="false"
android:focusable="false"
android:id="@+id/textView1"
android:text="@string/textView1_Text"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
</LinearLayout>
<LinearLayout
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<RadioButton
android:clickable="false"
android:focusable="false"
android:id="@+id/radioButton2"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
<TextView
android:clickable="false"
android:focusable="false"
android:id="@+id/textView2"
android:text="@string/textView2_Text"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent" />
</LinearLayout>
<!--more RadioButtons-->
</RadioGroup>
<!--...-->
Set up onClick()
listeners for the LinearLayout
s (not for RadioButton
s) in the onCreate()
procedure (don't forget to import android.view.View;
and import android.widget.RadioGroup;
):
//...
findViewById(R.id.linearLayout1).setOnClickListener(new View.OnClickListener()
{
public void onClick(final View objView)
{
((RadioGroup) findViewById(R.id.radioGroupBase)).check(findViewById(R.id.radioButton1).getId());
}
});
findViewById(R.id.linearLayout2).setOnClickListener(new View.OnClickListener()
{
public void onClick(final View objView)
{
((RadioGroup) findViewById(R.id.radioGroupBase)).check(findViewById(R.id.radioButton2).getId());
}
});
//more SetOnClickListener() calls for more LinearLayouts ("RadioButtons")
//...
All that's left is to set up an onCheckedChanged()
listener for the RadioGroup
in the same onCreate()
procedure:
//...
((RadioGroup) findViewById(R.id.radioGroupBase)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(final RadioGroup objRadioGroup, final int nCheckedID)
{
if (!objRadioGroup.isShown())
return;
//do something depending on which RadioButton was checked
}
});
//...
It might be a good idea to make a simple getter for the RadioGroup
and maybe for the LinearLayout
s as well (the latter would require import android.widget.LinearLayout;
):
//...
private RadioGroup Get_radioGroupBase() { return findViewById(R.id.radioGroupBase); }
private LinearLayout Get_linearLayout1() { return findViewById(R.id.linearLayout1); }
private LinearLayout Get_linearLayout2() { return findViewById(R.id.linearLayout2); }
//more getters for more LinearLayouts ("RadioButtons")
//...