I have a custom DialogPreference subclass in which I would like to have a RadioGroup with three radio buttons. The first two are vanilla RadioButton components with titles, but for the third I would like to place an EditText directly to the right of it so I can enter a custom value when that button is selected.
I have tried putting the third button into a horizontal LinearLayout along with the EditText but then the third button does not participate in the parent RadioGroup anymore.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/lactate_radio"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/lactate_default_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lactate_default_value"/>
<RadioButton
android:id="@+id/lactate_calibrated_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lactate_calibrated_value" />
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/lactate_custom_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lactate_custom_value"/>
<EditText
android:id="@+id/lactate_custom_value_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</RadioGroup>
</LinearLayout>

I have also tried adding this LinearLayout programmatically but still had the same behavior. Is there any way to do this, perhaps by explicitly telling the parent RadioGroup about the third button or by somehow positioning the EditText appropriately without using a horizontal LinearLayout? Otherwise, I think I'll have to write a RadioButton subclass which ought to be a real party!
To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
When we click on the radio button, the actionPerformed() method is called. Use ButtonGroup. getSelection(). getActionCommand() to get the value selected by the user.
For this, open the “MainActivity. java” file and instantiate the components made in the XML file (RadioGroup, TextView, Clear, and Submit Button) using findViewById() method. This method binds the created object to the UI Components with the help of the assigned ID. Button submit = (Button)findViewById(R.
You should be able to change your parent layout to RelativeLayout. Then give your RadioGroup
android:orientation="vertical"
then place your EditText next to and aligned with the bottom of the RadioGroup. I haven't tried it but something like this should work.
Update
I got time to test it. This seems to give the output you are looking for
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/lactate_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/lactate_default_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lactate_default_value"/>
<RadioButton
android:id="@+id/lactate_calibrated_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lactate_calibrated_value" />
<RadioButton
android:id="@+id/lactate_custom_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="lactate_custom_value"/>
</RadioGroup>
<EditText
android:id="@+id/lactate_custom_value_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/lactate_radio"
android:layout_toRightOf="@+id/lactate_radio"
android:text="Test Text" />
</RelativeLayout>
Note that I also changed some of the widths to wrap_content so it would fit together. Changing the orientation of the RadioGroup to vertical eliminates the need for the LinearLayout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With