Creating a ListPreference options menu - android

I'm trying to create an option menu in my preferences but I can't get the options to display. When I select the option in the prefs menu I got an empty pop up dialog (title at the top, button at the bottom, but no options).
This is what I do:
I create an array.xml file in res/values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listArray">
<item>3</item>
<item>5</item>
<item>7</item>
<item>10</item>
<item>15</item>
<item>20</item>
</string-array>
<string-array name="listValues">
<item>3</item>
<item>5</item>
<item>7</item>
<item>10</item>
<item>15</item>
<item>20</item>
</string-array>
and in m prefs.xml i have:
<ListPreference
android:dialogTitle="#string/prefsMaxAdAge"
android:dialogMessage="#string/prefsMaxAdAgeSummary"
android:key="itemMaxAdAge"
android:title="#string/prefsMaxAdAge"
android:summary="#string/prefsMaxAdAgeSummary"
android:entryValues="#array/listValues"
android:entries="#array/listArray">
</ListPreference>
Any help would be much appreciated! Thank you!

I finally found the solution to that problem.
The XML field DialogMsg must not be set. This makes sense after all.
Useful help was found here:
http://code.google.com/p/android/issues/detail?id=4497

ListPreferences seem to appear empty whenever they contain a dialogMessage. As to why, I would very much like to know the answer...

Related

Set constant choices in Android Spinner from layout.xml

I have a spinner in my layout that I would like to populate with pre-determined, hard-coded data.
Of course I can dynamically add the data from my Activity class, but I want to know if there is a way to do this in the xml itself.
All of the choices that I want to appear in the spinner are present in arrays.xml. Is there a way to plug in this data into the Spinner?
plug this into your spinner assuming that you have properly created you xml array...
android:drawSelectorOnTop="true"
android:entries="#array/array_name"
Your String Resources you should add the array like so...
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>
This worked for me with a string-array named “count” loaded from the projects resources:
Spinner spinnerCount = (Spinner)findViewById(R.id.spinner_counts);
ArrayAdapter<String> spinnerCountArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.count));
spinnerCount.setAdapter(spinnerCountArrayAdapter);
let me know if it will fulfill your requirements.
This is my resource file (res/values/arrays.xml) with the string array:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name=“count”>
<item>0</item>
<item>5</item>
<item>10</item>
<item>100</item>
<item>1000</item>
<item>10000</item>
</string-array>
</resources>

using image as entries in listpreference

I have used the listpreference in my preference screen. I wants to show the image in list entries. I have used the code but I am getting image address instead of image in entries. Help me with this problem.
Here is my code:
preferencecheckbox.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<ListPreference
android:key="listkey"
android:title="select"
android:summary="Select type"
android:entries="#array/array"
android:entryValues="#array/value"/>
</PreferenceScreen>
array.xml
<resources>
<array name="array">
<item>#drawable/image1</item>
<item>#drawable/image2</item>
<item>#drawable/image3</item>
</array>
<string-array name="value">
<item>data1</item>
<item>data2</item>
<item>data3</item>
</string-array>
</resources>

Values inside items in string-array xml

Is something like this possible??
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item value=My >Mercury</item>
<item value=Vs >Venus</item>
<item value=EA >Earth</item>
<item value=Ms >Mars</item>
</string-array>
</resources>
the listView will show the planets but when the user selects Mars
the value I get will be Ms.
No, there is no such direct way to use string array in android app resources. One of my suggestion is separate the label and value to two different string arrays, then use these string arrays in your code.
<string-array name="planet_label">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
<string-array name="planet_values">
<item>My</item>
<item>Vs</item>
<item>Ea</item>
<item>Ms</item>
</string-array>

Android translate navigation drawer item

I have a navigation drawer menu with diferent items in a listview. This item´s names are in arrays.xml like this one in values-es and other one in values:
<string-array name="nav_drawer_items">
<item>One</item>
<item>Two</item>
<item>Three</item>
<item>Four</item>
</string-array>
I have done a method to translate the app but this items not change when a different language is selected. The others textview work perfect. What I am doing wrong? it is possible to translate an array item?
Try this, but do not forget initially define variables.
<string-array name="nav_drawer_items">
<item>#string/one</item>
<item>#string/two</item>
<item>#string/three</item>
</string-array>

how to NAME a string-array using a string resource

How can I do this?
<string-array name="#string/a_string_from_resources">
Is it possible to name a string-array using a string Resource?
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="#string/a_string_from_resources">
<item>first</item>
<item>second</item>
<item>third</item>
<item>fourth</item>
<item>fifth</item>
</string-array>
</resources>
<string-array name="array_name">
My guess is you cant! Because the R.java is generated from all the resources at the same time. As the xmls are meaningless before generating, resource files wont be able to gather the resources from themselves.

Categories

Resources