I have the following problem:
I created a Char Sequence and was able to name 4 units. I would however rather use strings from my XML file for localization purposes. Is there any way to achieve that?
final CharSequence[] choices =
//want to add strings here i.e. R.strings.lemonade
{"Coke", "Pepsi" , "Sprite" , "Seven Up" };
builderType.setSingleChoiceItems( choices, selected, new OnClickListener()
{.......
Error message:
Type mismatch: cannot convert from int to CharSequence
There's another overload of AlertDialog.Builder.setSingleChoiceItems() that takes in an int resource id for a string array of items. Put the following in an xml in res/values e.g. strings.xml:
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
Then you can use it as:
builderType.setSingleChoiceItems(R.array.choices, selected, new OnClickListener(), ...
For generic cases, you can also load string array resources with Resources.getStringArray() as suggested by #Egor.
Create a string-array in strings.xml
<string-array name="choices">
<item>Coke</item>
<item>Pepsi</item>
<item>Sprite</item>
<item>Seven Up</item>
</string-array>
Then fetch it from resources
String[] choices = context.getResources().getStringArray(R.array.choices);
Then use it in setSingleChoiceItems() as is, since String implements CharSequence.
Related
<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>
I have a project (meaning an android app) created and used a custom ListView with three TextView UI for each column. But the app is also designed to support multi-language (at least three) using strings.xml file to fetch the selected language when user selects. Thus, the case here is, I have to write for the ListView String [] example {"a", "b", "c"} in java. I tried to do something like this String [] example {R.string.a, R.string. b, R.string. c} but the compiler says Error " illegal array ".
Now my question: Is it possible to reference the String [] { } of the ListView to strings.XML files.
Thank in advance and any example code or link I'm appreciated.
If your elements of the array are constant, you can make string array in strings.xml and access that array in code.
Put the following in strings.xml
<string-array name="example">
<item>a</item>
<item>b</item>
<item>c</item>
</string-array>
Access this array in code as:
String examples[] = getResources().getStringArray(R.array.example);
you can not use the direct reference to the String array, you have to get the value of String resource via getString()
like this
String[] example = {
getString(R.string.a), getString(R.string.b), getString(R.string.c)
};
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.
i want to get a string from strings.xml. i know how to do this. but my problem is something else:
i have a String Variable which changes every time, and every time it changes, i want to look at strings.xml and check if that String variable exists in Strings.xml, then get text.
for example:
String title="sample title" \\ which changes
String city= "sample city"
String s = getResources().getString(R.string.title);
in the third line: title is a String, and there isn't any "title" named String in Strings.xml
how can i do this? please help me
As far as I can tell, you could use public int getIdentifier (String name, String defType, String defPackage). Its use is discouraged, though.
To use it (I haven't done it but I had once read about the method) you probably would need to:
int identifier = getResources().getIdentifier ("title","string","your.package.name.here");
if (identifier!=0){
s=getResources().getString(identifier);
}
else{
s="";//or null or whatever
}
One thing you could do to get strings from dynamic keys is make 2 string arrays and put them in a HashMap.
arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="title_keys">
<item>title1</item>
<item>title2</item>
<item>title3</item>
</string-array>
<string-array name="title_values">
<item>Real Title 1</item>
<item>Real Title 2</item>
<item>Real Title 3</item>
</string-array>
</resources>
And in your code:
String[] titleKeys = getResources().getStringArray(R.array.title_keys);
String[] titleValues = getResources().getStringArray(R.array.title_values);
HashMap<String, String> titles = new HashMap<String, String>();
for(int i = 0; i < titleKeys.length; i++) {
titles.put(titleKeys[i], titleValues[i]);
}
Finally, to get your titles from a dynamic key:
titles.get(titleFromSomewhere);
You wouldn't. You use strings.xml for constant strings. What you want to do is one of two things.
1)You want a String to be constant, but one of a few options (for example, one item in a list of countries). In this case, put all of the options in strings.xml, and hold which one you're currently using in an int. When you need to get the actual string, use getString().
2)It really can be any string (for example, a user entered name). In that case it doesn't go in strings.xml at all, you just use a String variable.
This can not be done, resources are converted to unique ints in R.java, and those are used to look up your actual string resources.
So R.string.title is actually something like 0x78E84A34.
You can write your own class which manages strings for you utilizing a HashMap<String,String> to lookup full strings for shorter "key" strings.
I have a string array defined in xml:
<string-array name="question_array">
<item> q1 "First string"</item>
<item> q2 "Second string "</item>
</string-array>
and want to use SetText to access and display these strings. None of these variations work:
setText(R.array.question_array[counter]);
setText(R.string.question_array[counter]);
setText(R.array.question_array);
setText(R.string.question_array);
What is the proper syntax for doing this?
Resources r = getResources();
Duration = r.getStringArray(R.array.planets_array);//duration is array i created in my code
create your own array and use it in your code