Search code examples
androidxmlcheckboxradio-buttonpreferences

How to create RadioButton group in preference.xml window?


I'm beginner in Java Android developing. I'm using Eclipse SDK 3.6.1 version. I have a preferences window with two checkbox and one back button.

    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
             <PreferenceCategory
             android:title="PIN requirement">                
            <CheckBoxPreference                        
                    android:title="Use PIN" 
                    android:defaultValue="true"                                              
                    android:key="checkboxPref" /> 
           <CheckBoxPreference
                    android:title="Don't use PIN"
                    android:defaultValue="false"                        
                    android:key="checkboxPref2" />

                    </PreferenceCategory>

<PreferenceCategory>
    <Preference
            android:title="Back"                
            android:key="customPref" />
    </PreferenceCategory>
</PreferenceScreen>

How to change two CheckBox in to the RadioButton group?


Solution

  • If you need just to enable or disable using PIN, only one CheckBoxPreference will be enough in this case (see example code below, First Category). RadioButtons are usually used, when you need to choose something from a list of settings (ListPreference) - for example (see example code, Second Category), to pick a color.

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <PreferenceCategory
            android:title="First Category">
            <CheckBoxPreference
                android:title="Using PIN"
                android:defaultValue="false"
                android:key="checkboxPref" 
                android:summaryOn="Disable PIN" 
                android:summaryOff="Enable PIN"/>
        </PreferenceCategory>
    
        <PreferenceCategory
            android:title="Second Category">
            <ListPreference
                android:title="Pick your favourite color"
                android:key="listPref"
                android:defaultValue="4"
                android:entries="@array/listArray"
                android:entryValues="@array/listValues" />
        </PreferenceCategory>
    </PreferenceScreen>
    

    The source code for this example will be:

    public class PreferencesHelpExample extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    
        public static final String KEY_LIST_PREFERENCE = "listPref";
    
        private ListPreference mListPreference;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            addPreferencesFromResource(R.xml.preferences);
    
            // Get a reference to the preferences
            mListPreference = (ListPreference)getPreferenceScreen().findPreference(KEY_LIST_PREFERENCE);
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            // Setup the initial values
            mListPreference.setSummary("Current value is " + mListPreference.getEntry().toString());
    
            // Set up a listener whenever a key changes            
            getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
    
            // Unregister the listener whenever a key changes            
            getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);    
        }
    
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            // Set new summary, when a preference value changes
            if (key.equals(KEY_LIST_PREFERENCE)) {
                mListPreference.setSummary("Current value is " + mListPreference.getEntry().toString()); 
            }
        }
    }
    

    For ListPreference you will also need an arrays.xml file, which is located in the "values" folder:

    <?xml version="1.0" encoding="utf-8"?>
    
    <resources>
    
        <string-array name="listArray">
            <item>red</item>
            <item>orange</item>
            <item>yellow</item>
            <item>green</item>
            <item>blue</item>
            <item>violet</item>
        </string-array>
    
        <string-array name="listValues">
            <item>1</item>
            <item>2</item>
            <item>3</item>
            <item>4</item>
            <item>5</item>
            <item>6</item>
        </string-array>
    
    </resources>
    

    See also some great examples, working with PreferenceActivity - they helped me a lot:

    Android Preferences;
    How to create a group of RadioButtons instead of a list;
    How to display the current value of an Android Preference in the Preference summary?