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
Related
I want to use a predefined list of elements in the android app settings using a MultiSelectListPreference and a few values already predifined in an xml file.
This is how it looks like
XML-File
<resources>
<string-array name="myTestList">
<item>Item 1</item>
<item>Item 2</item>
</string-array>
</resources>
And in the preferences.xml I added
<PreferenceCategory app:title="Example Title">
<MultiSelectListPreference
android:entries="#array/myTestList"
android:entryValues="#array/myTestList"
android:summary="yes / no"
android:title="Example Title 2" />
</PreferenceCategory>
So far it works and with
val myPreference: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
var sets: MutableSet<String?> = HashSet()
sets = myPreference.getStringSet("myTestList", HashSet<String>()) as MutableSet<String?>
sets.add("Item 3")
myPreference.edit().putStringSet("myTestList", sets).apply()
System.out.println("New list" + myPreference.getStringSet("myTestList", HashSet<String>()) as MutableSet<String?>)
What it does is to take the values from the predefined xml, adds a new element, save it and it works fine when i do the system out.
But now my problem: this new list should now be displayed in the settings of the app and here I fail to understand what i have to do to display it instead of using
android:entries="#array/myTestList"
android:entryValues="#array/myTestList"
Which ofcause is not working as this just points to the xml which now got updated.
Hope its understandable what i mean - im a beginner and using this forum for the first time.
Thanks for your help
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"
I have a settings menu with a list preference from the sample settings activity in Android Studio.
<ListPreference
android:key="example_list"
android:title="#string/pref_title_add_friends_to_messages"
android:defaultValue="5"
android:entries="#array/pref_example_list_titles"
android:entryValues="#array/pref_example_list_values"
android:negativeButtonText="#null"
android:positiveButtonText="#null" />
In this list preference you can select 8 different things.
<string name="pref_title_add_friends_to_messages">Klasse</string>
<string-array name="pref_example_list_titles">
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>
<string-array name="pref_example_list_values">
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>
I want to use the value of the preference to display links which belong to the 8 different settings.
E.g.:
5 - google.com
6 - wikipedia.com
etc.
As a beginner, how do I get the value of my preference and how do assign the values to the links and put them in one variable, which changes when the preference is being changed?
<string-array name="pref_example_list_values"> will be used as value in the ListPreference. To get the current value, use:
ListPreference lp = (ListPreference) findPreference("example_list");
String currentValue = lp.getValue();
To get the value and display it in a text, have a TextView and set the text:
/* You might create 'if' statement for 8 times because of there are 8 different value in the ListPreference*/
TextView tv = (TextView) findViewById(R.id.textview1);
if (currentValue.equals("5")) {
// do your thing here, i.e. google.com
tv.setText("Welcome, 5!");
}
if (currentValue.equals("6")) {
// do your thing here, i.e. wikipedia.com
tv.setText("Welcome, 6!");
}
You can get the value of your preference like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String value = sharedPref.getString("example_list", "default value");
I don't understand the rest of your question. What does
how do assign the values to the links and put them in one variable, which changes when the preference is being changed?
mean?
Also, I find the documentation on preference settings very helpful.
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
:
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.