How to add Radio Button in PreferenceScreen in android?
How can I add a radioButton in the main PreferenceScreen in android ? I do not want to add ListPreference for the 开发者_如何学JAVAradio button option. Like CheckBoxPreference ,I want to add radio ButtonPrefernce in PreferenceScreen. Need Help!!!!!!!!!!!!
Rgds, Praween
Create a new resource folder named xml in the app folder Make use of ListPreference
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="@string/pref_key"
android:title="@string/pref_title"
android:defaultValue="@string/pref_default"
android:entryValues="@array/pref_values"
android:entries="@array/pref_options"
/>
</PreferenceScreen>
Create a resource file name arrays.xml in values resource folder
<resources>
<string-array name="pref_values" >
<item>popular</item>
<item>top rated</item>
</string-array>
<string-array name="pref_options" >
<item>popular</item>
<item>top rated</item>
</string-array>
</resources>
The pref_values acts as the keys for the options. While the pref_options will be the visible options. The default should be either of top rated or popular
Hope this helps
u can add a radiobutton to preferencescreen.
pref.xml is the preference xml.
<PreferenceScreen
android:key="prefkey"
android:title="SETTINGS"
android:summary="HELLO"
/>
Create a xml(radio.xml) with only radiobutton in it and place it in layout folder.
now in code:
onCreate(){
PreferenceScreen root=getPreferenceScreen();
addPreferencesFromResource(R.xml.prefs);
PreferenceScreen pref= PreferenceScreen) root.findPreference("prefkey");
pref.setWidgetLayoutResource(R.layout.radio);
}
Radio buttons must be in groups that's why you should a listpreference first. If you want to add only one radio button then use checkbox instead ;)
精彩评论