Android List Preferences - android

I have built the default SettingsActivity in Android Studio (v2.2.3). I would like to change one of the settings to a ListPreference to allow the user to enter a family name and birth date. (Also update and delete) I have the following XML but I am not sure how to modify it to get what I want which is in the attached image file.
<ListPreference
android:defaultValue="-1"
android:entries="#array/pref_example_list_titles"
android:entryValues="#array/pref_example_list_values"
android:key="example_list"
android:negativeButtonText="#null"
android:positiveButtonText="#null"
android:title="Add Family Member" />
Idealized Preferences

Related

how to display preference values

I'm trying to migrate the preferences part of my app because of warning
default constructor in android.preference.preferenceactivity is deprecated
But, even after reading a lot of ressources, I don't understand why I can't have 2 preference values (string and integer) displayed in front of (or below) the EditTextPreference as I had before.
"Compte" (in the capture) (String) is enabled="false" because I don't want the user can edit it. But I want the user can see the account name in preferences.
"Nom de jours" (Integer) can be edited, if the user click on it, but I want the value is shown without opening editing mode.
prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="#string/prefsCatGlobalTitle">
<EditTextPreference
app:enabled="false"
app:key="param_email"
app:title="#string/cnxEmailLabel" />
</PreferenceCategory>
<PreferenceCategory app:title="#string/prefsCatAlertsTitle">
<EditTextPreference
app:defaultValue="7"
android:inputType="number"
app:key="prefsAlertNbDays"
android:maxLength="2"
app:maxLines="1"
android:selectAllOnFocus="true"
app:singleLine="true"
app:title="#string/prefsAlertNbOfDays" />
<SwitchPreference
app:defaultValue="true"
app:key="prefsAlertNotif"
app:title="#string/prefsAlertNotifTitle" />
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertEmail"
app:title="#string/prefsAlertEmailTitle" />
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertCal"
app:title="#string/prefsAlertCalTitle" />
</PreferenceCategory>
<PreferenceCategory app:title="#string/prefsCatOthersTitle">
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertAutoSync"
app:title="#string/prefsAlertAutoSync" />
</PreferenceCategory>
</PreferenceScreen>
Preference capture
Apart from the visualization problem, everything works fine and my preferences are well saved and usable in my java code.
I can't find what I'm doing wrong or what I'm missing.
Thanks to #cmak
I never found this app:useSimpleSummaryProvider option before.

Android EditTextPreference default value not shown

I have a XML PreferenceScreen that holds an EditTextPreference like below
<EditTextPreference
app:key="preference_key"
app:title="test preference"
app:useSimpleSummaryProvider="true"/>
If the user has not set anything "Not set" is shown as expected. However I want this to have a default value which is also shown, but the value is dynamic based on some other settings. I already have the code to get said setting, but I just don't know how to set it in the EditTextPrefrence (preference_key) programmatically?
If i try with Preference.setDefaultValue it still showns "Not set"

Getting value from edittext preference in preference screen

I'm new to android development. I just heard about preference screen in android. So I just made one. This is just to enter name in a dialog.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="Name"
android:summary="To enter name click on Name"
android:dialogTitle="Name Dialog"
android:dialogMessage="Enter Your Name"
android:key="name"/>
This is my created preference screen. I just need to know how can I access the entered name 'tony' in my mainactivity. I don't know the way to access the name from the dialog. Do I need to add any seperate activity for accessing the name in the name dialog. Can someone please explain it to me.
Try this:
PreferenceManager.getDefaultSharedPreferences(this).getString("name", null);
Where this is a Context like an Activity.
name is the key defined in your xml file.

Android : Set a preference's summary in a main preference screen to the values selected in a sub preference screen

My requirement
Have a main screen full of preferences (main_screen)
One preference (pref1) in this main_screen when clicked upon opens a
sub screen of settings (sub_screen)
In this sub_screen, there are 2 ListPreferences, when the user
selects a value from these lists, the summary for that ListPreference
is updated to contain the value the user selected
in the main_screen, the summary for pref1 should show the values
selected in the subscreen's listPreferences (i.e. the summary has
List1SelectedValue, List2SelectedValue)
On going into the main_screen for the first time, the summary for
pref1 should be populated
On going to the sub_screen and changing the values, and then
returning to the main_screen the summary should be updated to reflect
the newly selected values in sub_screen.
I have searched around and i can not work out how to set the summary of the pref1 on the main screen to the values selected in the 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 for 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 for this main screen of
preferences
com.my.test.SubScreenPreferenceActivity : code for this sub screen
of preferences
What the screens look like
When the settings are displayed the user will see
Section 1 Heading
-----------------
Pref 1
Section 2 Heading
-----------------
Sub screen of settings
Clicking on "Sub screen of settings" will take you to a second settings screen that looks like the following
Additional Settings
-------------------
List Pref 1
Apples
List Pref 2
Cream
Clicking on "List Pref 1" will show a popup for the user to select Apples/Pears/Bananas
Clicking on "List Pref 2" will show a popup for the user to select Cream/Ice cream/Custard
In SubScreenPreferenceActivity i have registered an OnSharedPreferenceChangeListener so that when the user selects a value from one of the options popped up the summary for the the ListPreferences is updated with the value the user has selected.
What I am completely stuck on
I would like the main_screen to also contain a summary of the values that have been set in the sub_screen, for example, in the main screen i would like it to render like the following
Section 1 Heading
----------------
Pref 1
Section 2 Heading
------------------
Sub screen of settings
Apples, Cream
I would like it that when i go into the main_screen initially, the "Sub screen of settings" preference's summary is already set to the currently stored values for the preferences in the sub-screen (using the display values not the actual values).
Also when the user goes to the sub screen and changes the values, on returning to the main_screen the "Sub screen of settings" preference's summary is updated to show the new values of the settings.
How do i set the summary in the main_screen (MainPreferenceActivity) to the values selected in the sub_screen?
How do i update the main_screen when the preferences in the sub_screen (SubScreenPreferenceActivity) change?
Why I have the sub_screen xml in its own file and activity
By the way, I have the sub-screen in a separate XML file and with its own Activity class as I need to call it from the Android settings screens.
In the Android settings, when you click on the Account for my application it shows the "Account & Settings | Sync Settings" screen. In this screen i have it displaying the "Section 2 Heading" PreferenceCategory section (just like in my applications settings screen), clicking on "Account & Settings | Sync Settings" screen takes you to the sub-section preferences screen in my 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 this myself. It turned out to not be too complex.
In the "onResume" of the activity for the first preference screen, I simply call a utility method to generate the summary string for the preference whose summary is to contain the values of all selected values in the second screen. This utility method queries the stored preferences to get the preference values and then makes up a suitable string. As this utility method checks the values of the stored preference the summary will be accurate when you first go into the activity as well as when you return to the activity from the sub-screen.
For example
in "com.my.test.MainPreferenceActivity" "onResume" method i have the following
// update the preference's summary to a string containing the values selected in the sub-screen
Preference syncPref = findPreference(SUB_SCREN_OF_SETTINGS);
syncPref.setSummary(getSubScreenSummary(....));
public String getSubScreenSummary(){
// get the value of list_pref_1
// get the value of list_pref_2
String s = ...... // build up the string based on values of list_pref_1/list_pref_2
return s;
}

New activity from default preferences

I have setup my default android Preferences with the necessary options. The prime reason of the Preferences are to allow users to manage their resources. In the context of my app, they are "Contacts", "Types of projects" and "Currency".
The "Currency" section works fine. The display just yet, not the programming. But what I really want to do is, when either of the "Contacts" or "Type of projects" is clicked, they should open another custom activity to let the user manage his contacts. These are by the way, not the android contacts. It is an activity connected to a database table allowing users to manage his contacts and his type of projects.
Any advise on this please? Perhaps, I shouldn't be using the default android preferences at all?
This is my XML code for the Preferences:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="Manage your resources" >
<ListPreference
android:key="contacts"
android:title="Manage your Contacts"
android:summary="Click here to ADD, EDIT and DELETE Contacts" >
</ListPreference>
<ListPreference
android:key="projects"
android:title="Types of Projects"
android:summary="Click here to maintain a list of Types of Projects that match your profession" >
</ListPreference>
<ListPreference
android:key="currency"
android:title="Select currency"
android:summary="Set a default currency that you wish to use" android:entryValues="#array/entryvalues_list_preference" android:entries="#array/entries_list_preference">
</ListPreference>
</PreferenceCategory>
Thanks in advance....
Perhaps, I shouldn't be using the default android preferences at all?
IMHO, you "shouldn't be using the default android preferences" for "Contacts" and "Types of Projects", if you will be rendering a non-preference UI for them.

Categories

Resources