I have a settings menu with a list preference from the sample settings activity in Android Studio.
<ListPreference
android:key="example_list"
android:title="#string/pref_title_add_friends_to_messages"
android:defaultValue="5"
android:entries="#array/pref_example_list_titles"
android:entryValues="#array/pref_example_list_values"
android:negativeButtonText="#null"
android:positiveButtonText="#null" />
In this list preference you can select 8 different things.
<string name="pref_title_add_friends_to_messages">Klasse</string>
<string-array name="pref_example_list_titles">
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>
<string-array name="pref_example_list_values">
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>
I want to use the value of the preference to display links which belong to the 8 different settings.
E.g.:
5 - google.com
6 - wikipedia.com
etc.
As a beginner, how do I get the value of my preference and how do assign the values to the links and put them in one variable, which changes when the preference is being changed?
<string-array name="pref_example_list_values"> will be used as value in the ListPreference. To get the current value, use:
ListPreference lp = (ListPreference) findPreference("example_list");
String currentValue = lp.getValue();
To get the value and display it in a text, have a TextView and set the text:
/* You might create 'if' statement for 8 times because of there are 8 different value in the ListPreference*/
TextView tv = (TextView) findViewById(R.id.textview1);
if (currentValue.equals("5")) {
// do your thing here, i.e. google.com
tv.setText("Welcome, 5!");
}
if (currentValue.equals("6")) {
// do your thing here, i.e. wikipedia.com
tv.setText("Welcome, 6!");
}
You can get the value of your preference like this:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String value = sharedPref.getString("example_list", "default value");
I don't understand the rest of your question. What does
how do assign the values to the links and put them in one variable, which changes when the preference is being changed?
mean?
Also, I find the documentation on preference settings very helpful.
Related
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>
Maybe this is a bit complicated but I have an Kotlin Android Studio app that says the year and the month the car was registered based on the Letters of the 2 letters of the license plate.
The letters goes AA to ZZ (example: AA, AB, AC ... AZ, BA, BB ... ZX ZZ)
and I have in the file strings.xml the values stored
Example:
<string-array name='AP'>
<item>2005</item>
<item>9</item>
</string-array>
<string-array name='AQ'>
<item>2005</item>
<item>9</item>
</string-array>
<string-array name='AR'>
<item>2005</item>
<item>10</item>
</string-array>
<string-array name='AS'>
<item>2005</item>
<item>10</item>
</string-array>
<string-array name='AT'>
<item>2005</item>
<item>10</item>
</string-array>
<string-array name='AU'>
<item>2005</item>
<item>11</item>
</string-array>
Then, based in the user input on EditText checks the letters and finds on the Strings.xml
The problem is if I do
when (x) {
"AA" -> print("x == 1")
"AB" -> print("x == 2")
.....
}
will result in 529 lines of code just to check the the letters on the license plate...
My question is if there is an better and tidier way of doing this, like this:
val stringArray = resources.getStringArray(R.array."EditText.getText()")
Sounds complicated... and I'm an beginner in Kotlin
String array resources are constants. You can't call R.array.somevariable. You should use XML parser instead.
I am trying to create a ListView in android but it is only generating one item on the list.
Here is my code snippet
String[] kampala1 = getResources().getStringArray(R.array.kla);
sitelistview.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_activated_1,kampala1));
i need help in getting all strings in the string array I created
string array is
<string-array name="kla">
<item>uganda Museum</item>
<item>Namugongo Museum</item>
<item>wandegeya Market</item>
<item>kasubi tombs</item>
<item>kabaka palace</item>
<item>kabaka lake</item>
</string-array>
if you want to use hard code string array then use it directly using xml.
Declare your array in strings.xml like this.
<string-array name="kla">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
and use it in your layout.
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:entries="#array/kla"/>
You could use an array Adapter. Its a good solution with ample scope for flexibility and customisation. Check the example explained in the solution below.
http://stackoverflow.com/questions/2453989/help-in-getting-string-array-from-arrays-xml-file
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'm working with an application in Android and I have a list preference. The problem is that when I get the value with PreferenceManager.getDefaultSharedPreferences(context).getString(key, value) it returns the localized string instead of the entry value wich is what I'm expecting. In a previous version it was working. That is the right behavior? In that case, how shall i do to get the entry value? Here is some code to explain.
Thanks in advance.
Preferences.xml:
<ListPreference
android:id="#+id/protocol"
android:key="protocol"
android:title="#string/protocol"
android:dialogTitle="#string/change_protocol"
android:defaultValue="default"
android:entries="#array/protocol_entries"
android:entryValues="#array/protocol_entry_values" />
arrays.xml
<string-array
name="protocol_entry_values">
<item>default</item>
<item>protocol_1</item>
<item>protocol_2</item>
<item></item>
</string-array>
<string-array
name="protocol_entries">
<item>#string/label_default_protocol</item>
<item>#string/label_protocol_1</item>
<item>#string/label_protocol_2</item>
<item>#string/label_other</item>
</string-array>
strings.xml
<string name="label_default_protocol">Default protocol</string>
<string name="label_protocol_1">First protocol</string>
<string name="label_protocol_2">Second Protocol</string>
<string name="label_other">Other</string>
It sounds like you have different versions of the <string-array name="protocol_entry_values"> data in localized form. You should only have one definition of the values. The protocol_entries should be localized.