Why do I get empty string when I use getString function? - android

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.)

Related

Android Preferences defaultValue not works

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"

Android ListPreference default value not working

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

error: Error: String types not allowed (at 'entries' with value 'array/list')

I am new in android development and developing pref.xml (resource type prefrence) using eclipe software. Here is my pref.xml code
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="EditText"
android:key = "name"
android:summary="Enter Your name"
></EditTextPreference>
<CheckBoxPreference
android:title="CheckBox"
android:defaultValue="true"
android:key="checkBox"
android:summary="check this box"
></CheckBoxPreference>
<ListPreference
android:title="List"
android:key="list"
android:summary="This Is A List To Choose From"
android:entries="array/list"
></ListPreference>
and i got this error :
error: Error: String types not allowed (at 'entries' with value 'array/list')
Please help me.. how do i handle this error ?
Find file name strings.xml in res\value folder in your package explorer...
add following line in this file
<string-array name="list"></string-array>
Your Final file look like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Muzafar Khan</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="list"></string-array>
Now save your project... and enjoy :)
Change
android:entries="array/list"
to
android:entries="#array/list"
I am assuming you are trying to load the entries from the array resource file, correct? If so, you need to do "#array/list" instead. All of the resources (string, id, etc) must be prefixed with # in the XML.
This one worked for me
android:entries="#+array/list"
hope it helps :)
android:entries="#array/list"
gives an error, you have to use
android:entries="#+array/list
You need to create resource type 'arrays' in the 'res' folder where the array elements will be defined after declaring the array type in the pref_general.xml file.
In the arrays.xml file you can define as
<string-array name="**array name**">
<item>#string/**array element 1**</item>
<item>#string/**array element 2**</item>
</string-array>
So in your pref general file you will actually define array as
android:entryValues="#array/array name"
So in your string file you define the array elements as strings in normal way
Cheers
go to values package create a new xml file
for ex- array.xml
then
<resources>
<string-array name="list"></string-array>
set the string-array
and change
android:entries="array/list"
to
android:entries="#array/list"
for refering the file
:

Led notification preferences when battery is BATTERY_LOW?

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.

Why getDefaultSharedPreferences(context).getString(key, value) returns localized string?

I'm working with an application in Android and I have a list preference. The problem is that when I get the value with PreferenceManager.getDefaultSharedPreferences(context).getString(key, value) it returns the localized string instead of the entry value wich is what I'm expecting. In a previous version it was working. That is the right behavior? In that case, how shall i do to get the entry value? Here is some code to explain.
Thanks in advance.
Preferences.xml:
<ListPreference
android:id="#+id/protocol"
android:key="protocol"
android:title="#string/protocol"
android:dialogTitle="#string/change_protocol"
android:defaultValue="default"
android:entries="#array/protocol_entries"
android:entryValues="#array/protocol_entry_values" />
arrays.xml
<string-array
name="protocol_entry_values">
<item>default</item>
<item>protocol_1</item>
<item>protocol_2</item>
<item></item>
</string-array>
<string-array
name="protocol_entries">
<item>#string/label_default_protocol</item>
<item>#string/label_protocol_1</item>
<item>#string/label_protocol_2</item>
<item>#string/label_other</item>
</string-array>
strings.xml
<string name="label_default_protocol">Default protocol</string>
<string name="label_protocol_1">First protocol</string>
<string name="label_protocol_2">Second Protocol</string>
<string name="label_other">Other</string>
It sounds like you have different versions of the <string-array name="protocol_entry_values"> data in localized form. You should only have one definition of the values. The protocol_entries should be localized.

Categories

Resources