Android Spinner: Get Value from Selection - android

I am using a spinner populating with an xml file:
<string-array name="Spinner Items">
<item>Item 1: 2.0 - 4.0</item>
<item>Item 2: 1.0 - 3.0</item>
Is it possible to set a name value pair like this:
<string-array name="Spinner Items">
<item name="Item 1: 2.0 - 4.0" value="2"></item>
<item name="Item 2: 1.0 - 3.0" value="1.5"></item>
So that I can populate the spinner with the name and then when an item is selected it returns the value of that specific item? I figure it must be easier to do all this in the xml file as opposed to have to write if statement for each item when the spinner is changed. Of course the real xml file is much longer than this snippet.

The only way to do this is to implement a custom adapter-class.

I did it wrong way, but it works.
My Spinner(spinnerSex) contains Male/Female choice:
<string-array name="spinnerSex">
<item>Male</item>
<item>Female</item>
</string-array>
java code:
Object objSelectedItem = spinnerSex.getSelectedItem();
String strSelectedItem = String.valueOf(objSelectedItem);
String[] arrvals = this.getResources().getStringArray(R.array.spinnerValues);
boolean isMale = (strSelectedItem.equals(arrvals[0]));

Related

Update MultiSelectListPreference list programatically

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

How can i create nested array in android studio Strings.xml

<resources>
<string name="app_name">UnConv</string>
<string-array name="mainunit">
<item>Area</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>
</resources>
i want to add some other values to the above items like a subgroup. Example for area i want yard, acre etc how can i achieve that?
Unfortunately, There is no direct way to save two dimensional string-array in Android resource file. I provide 2 methods for replacement.
1. Save the data as Json string showing in the following code:
<string name="mainunit">{"Area":["yard","acre"],"Pressure":[],"Speed":[],"Volume":[]}</string>
Then parse Json String to Java/Kotlin Object.
2. Save subgroup value splitting with ",", and get value by position, but it has drawback because it ignored the key name.
<string-array name="mainunit">
<item>yard,acre</item>
<item>Pressure</item>
<item>Speed</item>
<item>Volume</item>
</string-array>

accessing string from string array in layout file of android studio

I am using Android studio and have an array in my string.xml file as:
<string-array name="my_array">
<item>text1</item>
<item>text2</item>
<item>text3</item>
</string-array>
I know how to access the array (and get the 1st item) in my MainActivity.java file:
myButton.setText(getResources().getStringArray(R.array.my_array)[0]);
My question: Is there anyway to set the text directly in the activity_main.xml file? I tried:
<Button
android:id="#+id/myButton"
android:text="#array/my_array[0]"
... />
but that causes an error. Without the "[0]" it displays the 1st value (text1), but maybe that is just because of the button's size and it's not showing the rest - I can't get it to display other items (e.g., text2).
Is it possible to access one value of the array directly in the layout file?
Thanks.
I found a good answer here: https://stackoverflow.com/a/4161645/933969
Basically you create named strings first (and use those where you would want mystrings[x]) and then create your array using references to those named strings:
<string name="earth">Earth</string>
<string name="moon">Moon</string>
<string-array name="system">
<item>#string/earth</item>
<item>#string/moon</item>
</string-array>

android adding MultiSelectListPreference

I am trying to implement android jetpack preference for my settings screen.
everything is working good, when I click on MultiSelectListPreference, it shows the list of entries, but I have few questions,
why can't the entryValues be a integer-array? (string-array is working fine)
how to set default values? for eg: I want to set the second and third entry to be checked by default in the beginning.
here is a part of my pref.xml file
...
app:entries="#array/res_entries"
app:entryValues="#array/res_id_values"
app:defaultValue="#array/res_def_values" //this line is not working
...
If I set res_id_values in arrays.xml file to be a integer-array, then app is crashing.
My settingsFragment class extends PreferenceFragmentCompat and override onCreatePreferences and in it, I have written
setPreferencesFromResource(R.xml.pref.xml, rootkey)
EDIT
My res_entries array :
<string-array name="res_entries">
<item>apple</item>
<item>Mango</item>
<item>Guava</item>
</string-array>
My res_id_values array :
<string-array name="res_id_values">
<item>1</item>
<item>2</item>
<item>12</item>
</string-array>
My res_def_values array :
<string-array name="res_def_values">
<item>true</item>
<item>false</item>
<item>true</item>
</string-array>
So finally, I solved it.
All thanks to #CommonsWare .
answer to my first question is that, we cannot use integer-array. Use a string array and store integer values in it. later when you get it, use Integer.parseInt().
now coming to second question, to store default value(let's say you want second and third item to be checked by default), use res_id_values in res_def_values. Don't use true/false or 0/1 like me.
eg: if in above question, If I want apple and guava to be checked by default, then my res_def_values will look like this:
<string-array name="res_def_values">
<item>1</item>
<item>12</item>
</string-array>

Get item value in arrayadapter

I want to get the value of the selected item in the spinner. I am using array adapter.
<string-array name="my_list">
<item value="">---Select the value from the List---</item>
<item value="value1">data1</item>
<item value="value2">data2</item>
<item value="value3">data3</item>
<item value="value4">data4</item>
<item value="value5">data5</item>
</string-array>
If I select the "data1" in my spinner, I want to get the "value1"..not "data1"
Anyone help me. Quick response helps me a lot. thanks in advance.
You have to add a values string-array like this instead of value attribute
<string-array name="my_list">
<item value="">---Select the value from the List---</item>
<item>data1</item>
<item>data2</item>
<item>data3</item>
<item>data4</item>
<item>data5</item>
</string-array>
<string-array name="my_list_values">
<item value="">---Select the value from the List---</item>
<item>value1</item>
<item>value2</item>
<item>value3</item>
<item>value4</item>
<item>value5</item>
</string-array>
To retrieve the values from the my_list_values, You need to write this in onItemSelected function in listener
String selectedValue = getResources().getStringArray(R.array.my_list_values)[parent.getSelectedItemPosition()];
You cann't do something like you have done directly as Android doesn't supports entryValues
I don't think that's possible, tried it myself a while back.
Maybe you just do what I did and create a second array holding the values you want on the same index?

Categories

Resources