Android : Set a preference's summary in a main preference screen to the values selected in a sub preference screen -
my requirement
- have main screen full of preferences (main_screen)
- one preference (pref1) in main_screen when clicked upon opens sub screen of settings (sub_screen)
- in sub_screen, there 2 listpreferences, when user selects value these lists, summary listpreference updated contain value user selected
- in main_screen, summary pref1 should show values selected in subscreen's listpreferences (i.e. summary has list1selectedvalue, list2selectedvalue)
- on going main_screen first time, summary pref1 should populated
- on going sub_screen , changing values, , returning main_screen summary should updated reflect newly selected values in sub_screen.
i have searched around , can not work out how set summary of pref1 on main screen values selected in sub_screen.
sample main_screen xml
<preferencescreen> <preferencecategory style="@style/settings_category_text" android:title="section 1 heading" > <preference android:key="section1_key1" android:title="pref 1"> </preference> </preferencecategory> <preferencecategory style="@style/settings_category_text" android:key="extra_settings_category" android:title="section 2 heading" > <preferencescreen android:key="sub_screen" android:title="sub screen of settings" android:summary=""> <intent android:targetpackage="com.my.test" android:targetclass="com.my.test.subscreenpreferenceactivity" /> </preferencescreen> </preferencecategory> </preferencescreen> sample sub_screen xml
<preferencescreen> <preferencecategory style="@style/settings_category_text" android:title="additional settings" > <listpreference android:key="list_pref1" android:title="list pref 1" android:defaultvalue="1" android:entries="@array/list_pref1_titles" android:entryvalues="@array/list_pref1_values" android:summary="%s" /> <listpreference android:key="list_pref2" android:title="list pref 2" android:defaultvalue="1" android:entries="@array/list_pref2_titles" android:entryvalues="@array/list_pref2_values" android:summary="%s" /> </preferencecategory> </preferencescreen> sample arrays list values in sub_screen
<string-array name="list_pref1_titles"> <item>apples</item> <item>pears</item> <item>bananas</item> </string-array> <string-array name="list_pref1_values"> <item>1</item> <item>2</item> <item>3</item> </string-array> <string-array name="list_pref2_titles"> <item>cream</item> <item>ice cream</item> <item>custard</item> </string-array> <string-array name="list_pref2_values"> <item>1</item> <item>2</item> <item>3</item> </string-array> my classes
- com.my.test.mainpreferenceactivity : code main screen of preferences
- com.my.test.subscreenpreferenceactivity : code sub screen of preferences
what screens like
when settings displayed user see
section 1 heading ----------------- pref 1 section 2 heading ----------------- sub screen of settings clicking on "sub screen of settings" take second settings screen looks following
additional settings ------------------- list pref 1 apples list pref 2 cream clicking on "list pref 1" show popup user select apples/pears/bananas
clicking on "list pref 2" show popup user select cream/ice cream/custard
in subscreenpreferenceactivity have registered onsharedpreferencechangelistener when user selects value 1 of options popped summary the listpreferences updated value user has selected.
what stuck on
i main_screen contain summary of values have been set in sub_screen, example, in main screen render following
section 1 heading ---------------- pref 1 section 2 heading ------------------ sub screen of settings apples, cream i when go main_screen initially, "sub screen of settings" preference's summary set stored values preferences in sub-screen (using display values not actual values).
also when user goes sub screen , changes values, on returning main_screen "sub screen of settings" preference's summary updated show new values of settings.
how set summary in main_screen (mainpreferenceactivity) values selected in sub_screen?
how update main_screen when preferences in sub_screen (subscreenpreferenceactivity) change?
why have sub_screen xml in own file , activity
by way, have sub-screen in separate xml file , own activity class need call android settings screens.
in android settings, when click on account application shows "account & settings | sync settings" screen. in screen have displaying "section 2 heading" preferencecategory section (just in applications settings screen), clicking on "account & settings | sync settings" screen takes sub-section preferences screen in application.
account & settings | sync settings appicon myaccount appname section 2 heading ----------------- sub screen of settings apples, cream data & synchronization ---------------------- account_authenticator.xml
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android" android:accounttype="myaccount" android:icon="@drawable/launcher" android:label="@string/app_name" android:smallicon="@drawable/launcher" android:accountpreferences="@xml/account_preferences"/> account_preferences.xml
<?xml version="1.0" encoding="utf-8"?> <preferencecategory style="@style/settings_category_text" android:key="extra_settings_category" android:title="section 2 heading" /> <preferencescreen android:key="sub_screen" android:title="sub screen of settings" android:summary=""> <intent android:targetpackage="com.my.test" android:targetclass="com.my.test.subscreenpreferenceactivity" /> </preferencescreen> </preferencescreen>
i have resolved myself. turned out not complex.
in "onresume" of activity first preference screen, call utility method generate summary string preference summary contain values of selected values in second screen. utility method queries stored preferences preference values , makes suitable string. utility method checks values of stored preference summary accurate when first go activity when return activity sub-screen.
for example
in "com.my.test.mainpreferenceactivity" "onresume" method have following
// update preference's summary string containing values selected in sub-screen preference syncpref = findpreference(sub_scren_of_settings); syncpref.setsummary(getsubscreensummary(....)); public string getsubscreensummary(){ // value of list_pref_1 // value of list_pref_2 string s = ...... // build string based on values of list_pref_1/list_pref_2 return s; }
Comments
Post a Comment