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.
Related
strings (English is the default)
<string-array name="languagesList">
<item>English</item>
<item>Arabic</item>
<item>Spanish</item>
<item>French</item>
<item>Chinese</item>
</string-array>
strings-ar
<string-array name="languagesList">
<item>الإنجليزية</item>
<item>العربية</item>
<item>الإسبانية</item>
<item>الفرنسية</item>
<item>الصينية</item>
</string-array>
strings-es
<string-array name="languagesList">
<item>inglés</item>
<item>Arábica</item>
<item>español</item>
<item>Francés</item>
<item>Chino</item>
</string-array>
strings-fr
<string-array name="languagesList">
<item>Anglais</item>
<item>arabe</item>
<item>Espagnol</item>
<item>Français</item>
<item>Chinois</item>
</string-array>
strings-zh
<string-array name="languagesList">
<item>英语</item>
<item>阿拉伯</item>
<item>西班牙语</item>
<item>法语</item>
<item>中国人</item>
</string-array>
Inside SettingsFragment user can change the app language, The buttons look like Chips, When the user clicks on Chip 1 I'll pass en, Chip 2 I'll pass ar, Chip 3 I'll pass es, etc...
I'll give the source code to the company and I don't know what languages they will add or remove, How can I make code responsive with any language?
Any ideas?
I know I can use string resource to localize item in string-array, just like Method 1 do.
Is it OK to localize the item in string-array directly ? just like Method 2
Thanks!
--------------------------Method 1-----------------------------------------
<string-array name="Box">
<item>#string/Inbox</item>
<item>#string/Sent</item>
<item>#string/Outbox</item>
<item>#string/Draft</item>
</string-array>
<string name="Inbox">Inbox</string>
<string name="Sent">Sent</string>
<string name="Outbox">Outbox</string>
<string name="Draft">Draft</string>
<string name="Inbox">收件</string>
<string name="Sent">发件</string>
<string name="Outbox">已发</string>
<string name="Draft">草稿</string>
-----------------------------Method 2-----------------------------------------
<string-array name="Box">
<item>Inbox</item>
<item>Sent</item>
<item>Outbox</item>
<item>Draft</item>
</string-array>
<string-array name="Box">
<item>收件</item>
<item>发件</item>
<item>已发</item>
<item>草稿</item>
</string-array>
yes you can use Method2.
you just have to create localized resource folder.
here is a complete reference.
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.
Im trying to set up a preferences but there is no options on the list here is the code
<ListPreference
android:title="list"
android:key="list"
android:summary= "This is a list to choose from"
android:entries="#array/list"
android:entryValues="#array/lvalues"
/>
Here is the code for the array
<string-array name="list">
<item Option="1"></item>
<item Option="2"></item>
<item Option="3"></item>
<item Option="4"></item>
</string-array>
<string-array name="lvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
I want it to say Option 1 etc next to the thing
Is this what you where trying to do?
<string-array name="list">
<item>Option 1</item>
<item>Option 2</item>
<item>Option 3</item>
<item>Option 4</item>
</string-array>
<string-array name="lvalues">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
String Array
An array of strings that can be referenced from the application.
Note: A string array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine string array resources with other simple resources in the one XML file, under one <resources> element.
ELEMENTS
<string-array>
Defines an array of strings. Contains one or more elements.
attributes:
name
String. A name for the array. This name will be used as the resource ID to reference the array.
<item>
A string, which can include styling tags. The value can be a reference to another string resource. Must be a child of a <string-array> element. Beware that you must escape apostrophes and quotation marks.
No attributes.
This application code retrieves a string array:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
Reference/Source:
http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
I know how to use strings.xml for global strings in Android, but what's about the String[] ?
Is there any possibility to use this and if there is, how?
Thank you.
If you meant String arrays inside and xml refer here
Create a file in your values folder called arrays.xml and create your array like this
<string-array name="colors">
<item>red</item>
<item>orange</item>
<item>yellow</item>
<item>green</item>
<item>blue</item>
<item>violet</item>
</string-array>
Not Directly in strings.xml, but something like this can be done in string.xml:
<string name="string1">str1</string>
<string name="string2">str2</string>
<string-array name="system">
<item>#string/string1</item>
<item>#string/string2</item>
</string-array>