Android translate navigation drawer item - android

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>

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>

Android Set xml value with string

I have this following spinner in my main activity
My problem is that the spinner's value is required when submitting but i want its default value to be empty so i put in my arrays.xml an item with empty value. so when the app is lunched it will show a empty spinner.
The problem is that when they click the spinner and the drop down display the first selection is empty.
my solution is to put the first data as "Select" but is there a way that the value would be empty like in html select tag?
<option value="">Select</option>
<option value="data 1">data 1</option>
on android xml is this possible? or how to do it? i tried this but still not working. it passes the value "Select"
<item value="">Select</item>
<item value="data 1">data 1</item>
my spinner
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/spinner_arr"
android:tag="relation" />
my arrays.xml
<string-array name="spinner_arr">
<item></item>
<item>data 1</item>
<item>data 2</item>
<item>data 3</item>
<item>data 4</item>
<item>data 5</item>
</string-array>
Try using the prompt attr of Spinner, if that's not work you'll have to craete some adapter of your own.
take a look here:
How to make an Android Spinner with initial text "Select One"

Android Spinner Option Value Just like Select Tag

Is it Possible I can create a spinner with options and values.
<select name=test>
<option value="1">Baran</option>
<option value="2">Khan</option>
</select>
with the spinner XML:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test">
<item Value="1">Baran</item>
<item value="2">Khan</item>
</string-array>
</resources>
How can I accomplish such target. As I need to pass Ids to the Server.
You have to manage two list and both are dynamic as you want.
Step to achieve :
Create two ArrayList<String>.Depend on your data type here i make as String Array.
Add value to ArrayList.
Create custom adapter and pass two list adapter in that and get value according that.
Add list to Spinner Adapter. Get the index or position of
the Spinner.
Follow same index to get value from second list value.
Send that value to server.
Task over
See Demo Example that will Guide you to make easy.
Enjoy !!!
Not the best but one approach would be to create another string array with the ids:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="test">
<item Value="1">Baran</item>
<item value="2">Khan</item>
</string-array>
<string-array name="testIDS">
<item>1</item>
<item>2</item>
</string-array>
</resources>
Now when item i is selected from array test you can get the id from item i in array testIDS.

Creating a ListPreference options menu

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

Is it possible to refer to Array Resource Items directly in XML?

In code, it's easy to use the Resources class to get an instance of an XML typed array resource and traverse its items. My question: is it possible to reference array resource items in XML itself as shown below
<resource>
<array name="items">
<item>Item One</item>
<item>Item Two</item>
<item>Item Three</item>
</array>
<string name="itemThree">#array/items[2]</string>
</resource>
The format shown above does not work. Does anyone know if that is possible using a different format?
No, but I think the reverse works. Define your strings as string resources and refer to them as #string/... in the <item> elements.
I would just define itemThree in Java:
String itemThree = getResources().getStringArray(R.array.items)[2]
Ultimately, the XML gets inflated into Java objects, so it's not much difference IMO.

Categories

Resources