Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no out-of-the-box RadioGroup preference available. You would need to write one, by extending DialogPreference class.</p> <p>Create a separate layout file (/res/layout/pref_radiogroup.xml) with the Dialog content.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;RadioGroup android:id="@+id/radioSex" android:layout_width="wrap_content" android:layout_height="wrap_content" &gt; &lt;RadioButton android:id="@+id/radioMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/radio_male" /&gt; &lt;RadioButton android:id="@+id/radioFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/radio_female" /&gt; &lt;/RadioGroup&gt; &lt;/FrameLayout&gt; </code></pre> <p>Create your RadioGroupPreference class which will extend DialogPreference.</p> <pre class="lang-java prettyprint-override"><code>public class RadioGroupPreference extends DialogPreference { private RadioGroup radioGroup; public RadioGroupPreference(Context context, AttributeSet attrs) { super(context, attrs); setDialogLayoutResource(R.layout.pref_radiogroup); } @Override protected View onCreateDialogView() { View root = super.onCreateDialogView(); radioGroup = (RadioGroup) root.findViewById(R.id.radioSex); return root; } @Override protected void onBindDialogView(View view) { super.onBindDialogView(view); setSelectedValue(getPersistedString("Male")); } @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (!positiveResult) { return; } if (shouldPersist()) { String valueToPersist = getSelectedValue(); if (callChangeListener(valueToPersist)) { persistString(valueToPersist); } } } @Override protected Object onGetDefaultValue(TypedArray a, int index) { return a.getString(index); } private void setSelectedValue(String value) { for (int i = 0; i &lt; radioGroup.getChildCount(); i++) { View view = radioGroup.getChildAt(i); if (view instanceof RadioButton) { RadioButton radioButton = (RadioButton) view; if (radioButton.getText().equals(value)) { radioButton.setChecked(true); } } } } private String getSelectedValue() { int radioButtonId = radioGroup.getCheckedRadioButtonId(); RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonId); return (String) radioButton.getText(); } } </code></pre> <p>Now you only have to add your custom preference to your preference screen.</p> <pre class="lang-xml prettyprint-override"><code>&lt;com.package.RadioGroupPreference android:summary="Radio Group Preference Summary" android:title="Radio Group Preference" android:key="preference_key" android:defaultValue="@string/radio_male" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload