Here is my preferences.xml
<PreferenceCategory android:title="#string/pref_cat_recognition">
<ListPreference
android:entries="#array/unitsArray"
android:entryValues="#array/unitsValues"
android:icon="#null"
android:key="unit"
android:summary="#string/units_summary"
android:title="#string/units"
android:defaultValue="1"/>
</PreferenceCategory>
and my array.xml
<string-array name="unitsArray" translatable="false">
<item>cm</item>
<item>inch</item>
</string-array>
<string-array name="unitsValues" translatable="false">
<item>1</item>
<item>2</item>
</string-array>
So it should be cm checked on start but there is no value checked. Why and how to solve that to have a value cm on start?
Your code is working fine. The reason why you dont see the selected value on the summary is because you have specified a custom summary text. You can use useSimpleSummaryProvider to enable the simple version of summary, also enabling the selected value seen without opening the preference.
Pls check my code below for clarity of things:
`<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="Choose Gender">
<ListPreference
app:defaultValue="m"
app:entries="#array/gender_entries"
app:entryValues="#array/gender_values"
app:key="gender"
app:title="Your Gender"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="StackOverflow">
<ListPreference
app:defaultValue="1"
app:entries="#array/unitsArray"
app:entryValues="#array/unitsValues"
app:key="unit"
app:title="Units"
app:useSimpleSummaryProvider="true" />
<ListPreference
app:defaultValue="1"
app:entries="#array/unitsArray"
app:entryValues="#array/unitsValues"
app:key="unit"
app:summary="Your summary goes here"
app:title="Units" />
</PreferenceCategory>
`
This is the preference screen view
If the summary was provided you'd be seeing the summary text and not the default/selected value like in the picture above.
It turned out in manifest I had android:allowBackup="true"
Related
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.
This is my code
<ListPreference
android:key="yearlength"
android:title="Year Length"
android:entries="#array/years"
android:entryValues="#array/yearsvalues"
android:summary="%s"
android:defaultValue="365.256363004"/>
<string-array name="years">
<item>Mean sidereal solar year(365.256363004)</item>
<item>Mean tropical solar year(365.24219)</item>
<item>Savana year(360)</item>
<item>Thithi year(354)</item>
<item>Nakshatra year(324)</item>
<item>Normal solar year(365.2425)</item>
</string-array>
<string-array name="yearsvalues">
<item>365.256363004</item>
<item>365.24219</item>
<item>360</item>
<item>354</item>
<item>324</item>
<item>365.2425</item>
</string-array>
I am new to andorid.The default value is not showing.
But when i set andorid:defaultValue="360" its working.plz help me
you should use string reference.
<string name="default>365.256363004</string>
and use
<ListPreference
android:key="yearlength"
android:title="Year Length"
android:entries="#array/years"
android:entryValues="#array/yearsvalues"
android:summary="%s"
android:defaultValue="#string/default"/>
<ListPreference
android:key="yearlength"
android:title="Year Length"
android:entries="#array/years"
android:entryValues="#array/yearsvalues"
android:summary="%s"
android:defaultValue="365.256363004"/>
Dont fotget to Uninstall or clear application data and reinstall the app.
The default value inf is parsed as a float, converted to a String, and then stored as the Preference default value.
Add
android:defaultValue="inf"
More Details Beware of Preference Default Values in XML
How can i create a simple radio button preferences to choose which led color use in BATTERY_LOW condition?
To change the led color:
Check this link
For the BATTERY_LOW you will ned a broadcastreceiver:
Check this link
Ok.
First, create a file in the values folder. Call it something like "preference_res.xml". Then add the following:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="prefColor">
<item>None</item>
<item>Blue</item>
<item>Green</item>
<item>Yellow</item>
<item>Other Color</item>
</string-array>
<string-array
name="prefColorVal">
<item>-1</item>
<item>-2</item>
<item>79</item>
<item>24</item>
<item>25</item>
<item>27</item>
<item>80</item>
</string-array>
</resources>
(Be aware that the prefColorVal items should ref to the value you want the radio button to have. The values shown here are random.)
In your preference.xml, add something like this:
<ListPreference
android:dialogTitle="Choose color"
android:entries="#array/prefColor"
android:entryValues="#array/prefColorVal"
android:key="bat_below_15"
android:negativeButtonText="Cancel"
android:positiveButtonText="Save"
android:summary="Choose color when below 15"
android:title="Color when below 15" />
If you don't know how to get or use these values, here are some pointers, but you should google this stuff:
preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
and use this to get the value:
preferences.getString("bat_below_15", Blue));
Hope it helps.
When I run the app for the first time, the value of dateFormat is empty,
I'm very surprised, the dateFormat should return default vale"HH:mm dd-MM-yyyy"
SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String dateFormat=dateFormat("SetDate", "HH:mm dd-MM-yyyy");
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="AppPreference"
android:summary="#string/Preferencesummary"
android:title="#string/Preference" >
<ListPreference
android:defaultValue="\n"
android:dialogTitle="#string/DateFormat"
android:entries="#array/Mydate"
android:entryValues="#array/Mydate_values"
android:key="SetDate"
android:summary="#string/DateFormatsummary"
android:title="#string/DateFormat"
android:layout="#layout/mypreference_layout"
/>
</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Mydate">
<item>HH:mm dd-MM-yyyy</item>
<item>HH:mm MM-dd-yyyy</item>
<item>yyyy-MM-dd HH:mm</item>
</string-array>
<string-array name="Mydate_values">
<item>HH:mm dd-MM-yyyy</item>
<item>HH:mm MM-dd-yyyy</item>
<item>yyyy-MM-dd HH:mm</item>
</string-array>
</resources>
Well this looks like the problem to me:
android:defaultValue="\n"
Why would you expect a default value of "HH:mm dd-MM-yyyy" when you're explicitly setting it to "\n"? I suggest you change the default value, and see if that works...
(I'm not an Android developer, so maybe I'm missing something... but that seems like the obvious approach to me.)
I appear to be the only person on the internet who can't figure this out, but when I use
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
what is the syntax of the XML file for the default settings. I simply want to set two preferences to true.
I have tried the normal android string resource format, but nothing seems to work. Thanks ahead of time.
Use android:defaultValue like so:
<CheckBoxPreference
android:key="my_pref"
android:defaultValue="true"
android:title="#string/pref_title_my_pref"
android:summary="#string/pref_summary_my_pref"/>
You are not the only person who doesn't know this. Even am struggling to know the file format of the XML. Finally i found the answer! There you go!
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference android:key="CLIENT_HOMESCREEN_TITLE"
android:defaultValue="Home Screen"/>
<EditTextPreference android:key="CLIENT_ADMIN_BUTTON"
android:defaultValue="Admin"/>
<EditTextPreference android:key="CLIENT_PLAYER_BUTTON"
android:defaultValue="Player"/>
<EditTextPreference android:key="CLIENT_SAVE_BUTTON"
android:defaultValue="Save"/>
<EditTextPreference android:key="CLIENT_CANCEL_BUTTON"
android:defaultValue="Cancel"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_IP"
android:defaultValue="IP:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_PORT"
android:defaultValue="Port:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_TITLE"
android:defaultValue="Server Setup"/>
</PreferenceScreen>
what is the syntax of the XML file for the default settings. I simply want to set two preferences to true.
For this just set default value to true
Whatever default values you have provided in preference xml file, that will be set.