android adding MultiSelectListPreference - android

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>

Related

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>

how to write multi language code for dropdown list in spinner

I want to write this soil name in local language but in database it should in English.
Normally for string name it works but for dropdown list what to do ?
please ,tell me solution..
Choose a Soil_Type
<string-array name="soiltype_arrays">
<item>Select Item</item>
<item>Alluvial</item>
<item>Black</item>
<item>Laterites Lateritic</item>
<item>Red</item>
<item>Forest Hill</item>
<item>Peaty and Marshy</item>
</string-array>
You can do it the same way by placing the string arrays in the different language folders
http://developer.android.com/training/basics/supporting-devices/languages.html

Android: how to make string-array to be a reference to another string-array?

I can make string to be a reference to another string just like that:
<resources>
<string name="my_string">My String</string>
<string name="my_alias">#string/my_string</string>
</resources>
And i tried to make string-array reference the same way:
<string-array name="my_string_array">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="my_string_array_alias">#array/my_string_array</string-array>
But this does not work, unfortunately. I tried to use this array and alias in ListPreference:
<ListPreference
android:key="my_key"
android:title="#string/my_title"
android:entries="#array/my_string_array_alias"
android:entryValues="#array/my_string_array"
android:dialogTitle="#string/my_dialog_title" />
and entries is just an empty array. Of course, i can make both entries and entryValues to take the same array, but i like it to be a different ones.
Found this on the Android dev site.
Note: Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.
It's a little vague but I believe this means the framework doesn't support it.

Remove Array Item from ListPreference

I have a List Preference for Accelerometer mode in my preference activity which I'm loading purely from xml. If a users device does not have or support an Accelerometer I wont to completely remove the item in the array. Do I have to create a separate list preference for this one option to remove it? (I would prefer to keep them all in one.) How can it be removed with the below setup?
Thanks for the tip.
Jason
<string-array name="mode_text">
<item>Others Modes</item>
<item>Others Modes</item>
<item>Accelerometer Mode</item>
</string-array>
<string-array name="mode_values">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>
When you say you want to "completely remove the item in the array", do you mean you want the ListPreference to not show in the PreferenceScreen? Or you want it to return a null value when you try to get the set value?
Okay, I understand now. I think you can't remove just that item in the array. You have to set new arrays for the entries and entryValues for your ListPreference, programatically.
You find the ListPreference by its key.
Here is an example:
if (accelerometerNotSupported) {
ListPreference accelMode = (ListPreference) findPreference("acceleratorMode");
accelMode.setEntries(new String[]{"Others Modes","Others Modes"});
accelMode.setEntryValues(new String[]{"0", "1"});
}
That will change your list preference to have only those two options.

Categories

Resources