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.
Related
I'm working with an android spinner. Here I have two arrays in my XML like below and I'm showing car array in mine spinner.
<string-array name="car">
<item>128i Coupe</item>
<item>M3 Coupe</item>
<item>M5 Sedan</item>
</string-array>
<string-array name="value">
<item>1</item>
<item>0</item>
<item>2</item>
</string-array>
Now I can call the any of them in my Java file like this
String[] BMW_Model = MainActivity.this.getResources().getStringArray(R.array.car);
I can get/print the value of this item on click by this way
String td = spinnerManufacture.getSelectedItem().toString();
Now what I need is when I click on an item of my car array I should display the value of my second value array according to the position. As an example.
If I click on M5 Sedan from my spinner item it should show 2 in a Toast message from my second array.
Any kind of suggestion will be highly appreciated.
Since you have completely different arrays, the solution to your problem could be to get the position of the pressed spinner element and get the value of the second array at this index.
int position = spinner.getSelectedItemPosition()
String[] valueArray = context.getResources().getStringArray(R.array.value);
Toast.makeText(context, valueArray[position],Toast. LENGTH_SHORT).show();
If you need to display toast on click, then you need to set clickListener
spinner.setOnItemSelectedListener(new
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>
I wanna implement custom sounds in Settings. For this i'm using ListPreference. As we know
android:entries - The human-readable array to present as a list.
android:entryValues - The array to find the value to save for a preference when an entry from entries is selected.
<ListPreference
android:key="sound_income"
android:title="#string/sound_income"
android:entries="#array/sound_income"
android:entryValues="#array/sound_income"
android:summary="Cutting through"/>
<string-array name="sound_income">
<item>Cutting through</item>
<item>Don don</item>
<item>Oringz pack nine</item>
<item>Oringz pack</item>
</string-array>
Right now shared preference saves above texts but in order to play sound i need to get resId like this R.raw.cutting_through. The question is that how can i implement android:entryValues="#array/sound_income_values" string array to create sound`s int value not their names?
I feel like i have been searching every where without finding an answer to this.
I have a list preference
<ListPreference
android:key="signedUpCompetetion"
android:entries="#array/listArray"
android:entryValues="#array/listValues"
/>
With the following entries and entryValues:
<resources>
<string-array name="listArray">
<item></item>
<item></item>
<item></item>
</string-array>
<string-array name="listValues">
<item></item>
<item></item>
<item></item>
</string-array>
</resources>
So say that in one of my activities i want to add something to the list.
The following is an attempt but it doesnt seem to work:
this.appPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
this.prefEditor = appPrefs.edit();
ListPreference list = (ListPreference) getSharedPreferences("signedUpCompetetion", MODE_PRIVATE);
list.setEntries("This is a test");
Anyone able to tell me how to add to a listpreference?
Update
By the looks of it this seems to work
PreferenceScreen root = this.getPreferenceScreen();
ListPreference list = (ListPreference) root.findPreference("signedUpCompetetion");
CharSequence[] entries = { "One", "Two", "Three" };
CharSequence[] entryValues = { "1", "2", "3" };
list.setEntries(entries);
list.setEntryValues(entryValues);
However there is 1 problem! if i restart the application the list is not preserved. which means that the list is empty!
Use setEntries(int) and setEntryValues(int)
list.setEntries(R.array.listArray);
list.setEntryValues(R.array.listValues);
Update for how to persist the data:
You can persist the data in several ways, the simplest one would be to call setPersistent(true):
list.setPersistent(true);
I wish to fill in a spinner control with MsgName field, and get MsgValue when I select a item of the spinner.
so I write the following code.but I don't think it's a good code, is there the better code?
Do I need to define two dimension string array for my app? How can I do? Thanks!
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<string-arrayname="MsgName">
<item>Inbox</item>
<item>Sent</item>
</string-array>
<string-arrayname="MsgValue">
<item>content://sms/inbox</item>
<item>content://sms/sent</item>
</string-array>
</resources>
I would want to explicitly associate each name with it's value, instead of just having them be in the same position in two separate arrays.
There's at least a couple of ways to accomplish this.
Use some sort of separator to concatenate the name and the value in the same item:
e.g.:
<string-array name="message">
<item>Inbox|content://sms/inbox</item>
<item>Sent|content://sms/sent</item>
</string-array>
Create a custom xml resource, e.g. res/values/message-list.xml:
<messages>
<message name="Inbox">content://inbox</message>
<message name="Sent">content://sent</message>
</messages>
Of course, either way, you're going to have process the contents and create a SpinnerAdapter - in the first case, you'll have to split the entries, and in the second case, you'll have to parse the xml.