Android getString issue - android

So I have many strings in strings.xml, they are recorded in a format of:
<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......
Now I want to load them one by one without having to type all the string IDs. I want to load them in a fashion like:
for (int i = 1; i <= num; i++) {
// Just showing what I mean.
String xxx = getString(R.string.("list_" + i));
}
Is there a method to do so?

Try this:
int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
String xxx = getString(resourceID);

It would be worth to use resource arrays. For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
and you can access, iterate as follows:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);

Related

Trying to get drawable resource ID from its name in Android

I have a resource file like this:
strings.xml
<string name="category01">My Category 01</string>
<string name="category02">My Category 02</string>
<string name="category03">My Category 03</string>
<string-array name="array_category_01">
<item name="title">#string/category01</item>
<item name="image">#drawable/img01</item>
</string-array>
<string-array name="array_category_02">
<item name="title">#string/category02</item>
<item name="image">#drawable/img02</item>
</string-array>
<string-array name="array_category_03">
<item name="title">#string/category03</item>
<item name="image">#drawable/img03</item>
</string-array>
<string-array name="categories_array">
<item>#array/array_category_01</item>
<item>#array/array_category_02</item>
<item>#array/array_category_03</item>
</string-array>
Note: #drawable/img01, #drawable/img02 and #drawable/img03 are png images locates at res\drawable folder
Later I perform below iteration to retrieve category-images pairs:
Resources res = this.context.getResources();
TypedArray ta = res.obtainTypedArray(R.array.categories_array);
int n = ta.length();
String[][] array = new String[n][];
for (int i = 0; i < n; ++i) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
array[i] = res.getStringArray(id);
} else {
// something wrong with the XML
}
}
ta.recycle();
so for example at the end I get an array which contents are:
array[0]
|
---> [0] "My Category 01"
|
---> [1] "res/drawable-xxhdpi-v4/img01.png"
array[1]
|
---> [0] "My Category 02"
|
---> [1] "res/drawable-xxhdpi-v4/img02.png"
array[2]
|
---> [0] "My Category 03"
|
---> [1] "res/drawable-xxhdpi-v4/img03.png"
It is just what I am expected to get, no problem until here.
Later in my code, I need to get the resource ID of one of the categories contained in the above array in order for set the content of an ImageView object so I perform below (let's say I want the image for category 2):
ImageView imageView = (ImageView) rowView.findViewById(R.id.categoryicon);
int resID = this.context.getResources().getIdentifier(array[1][1] , "drawable", this.context.getPackageName());
imageView.setImageResource(resID);
My problem is that resID is zero so it means drawable resource has not been found...
In order to work I need image names in the array to be img01, img02, img03 instead of "res/drawable-xxhdpi-v4/img01.png", "res/drawable-xxhdpi-v4/img02.png" and "res/drawable-xxhdpi-v4/img03.png" respectively so when I pass the names to getIdentifier through array[x][y] it works.
How can I get rid of this?
One simple solution would be to parse the string and get only the image name as below.
String[] Image1 = array[1][1].split("/");
String s = Image1[Image1.length-1].replace(".png","").trim();
int resID = this.context.getResources().getIdentifier(s , "drawable", this.context.getPackageName());
array[1][1] is the path to the resource. You must specify a name.See http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29

How to read a String Array from attrs.xml

I am implementing a custom view and want to define an array of strings in attrs.xml. Currently I am doing it as follows:
In attrs.xml file I have defined:
<attr name="twCells" format="string"/>
In activity_main.xml I am providing following data:
app:twCells="A1,A7,G1,G7"
And finally in MyCustomView.java file I am reading twCell attribute as following:
private List<String> twCells = null;
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BoardView, 0, 0);
twCells = Arrays.asList(a.getString(R.styleable.BoardView_twCells).split(","));
My question: Is there any better way available to do this?
If you agree to move your strings resources into the strings.xml file, you can do like this:
in strings.xml
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
and to get a string:
String planet = getResources().getStringArray(R.array.planets_array)[i];

Format an array of strings with arguments

I'd like to format an array of strings just like android used to format strings:
Usually we do:
strings.xml
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In some java code:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
I'm looking for something like:
in some arbitrary xml:
<string-array name="employee">
<item>name: %1$s</item>
<item>post: %2$s</item>
</string-array>
in some java code:
Resources res = getResources();
String[] employee = ArrayString.format(res.getStringArray(R.string.employee), name, post);
Is there an elegant way to do that?
EDIT:
The next pieces of code is a workaround and I'm posting it just to help #Sufian, who asked for it in a comment. It's not a real answer once my question is about format the string array's content and the bellow code is formatting each string separately.
In some misc.xml:
<string-array
name="string_array">
<item>1st position: %1$d</item>
<item>2nd position: %1$d</item>
</string-array>
Then, in java code:
res = getResources();
String[] sa = res.getStringArray(R.array.string_array);
for (int i = 0; i < sa.length; i++ ) {
text += String.format(sa[i], i);
}
Just use:
String text = String.format(res.getStringArray(R.array.myStringArray)[index], param1, param2);
getQuantityString may solve your problem.
Look at quantity strings in http://developer.android.com/guide/topics/resources/string-resource.html
Here's the specific API doc:
http://developer.android.com/reference/android/content/res/Resources.html#getQuantityString(int,%20int,%20java.lang.Object...)

How getting an array of xml file from android resource?

I have this array into my resource file :
<array name="xml_data">
<item>#xml/data1</item>
<item>#xml/data2</item>
<item>#xml/data3</item>
<item>#xml/data4</item>
</array>
Normally, it's not different from an normal array, but when getting in code, this doesn't work...
final Resources res = getResources();
int[] xmlList = res.getIntArray(R.array.xml_data);
Log.i(TAG, "Data found: "+ xmlList.length);
for (int i = 0; i < xmlList.length; i++) {
Log.i(TAG, "Extract xml id="+ xmlList[i].);
}
Here is the output obtained in the logcat :
Data found: 4
Extract xml id=0
Extract xml id=0
Extract xml id=0
Extract xml id=0
Can you help me about this?
Thanks.
For your convenience lets take an example:
I had a simple 'string' array( not int) in a sample xml file. Lets call it array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="doing">
<item>1</item>
<item>2/item>
<item>3/item>
<item>3</item>
</string-array>
</resources>
Now in my Java file, I called it as follows inside my OnCreate() function and it worked:
String[] xmlList = getResources().getStringArray(R.array.doing);
int first = Integer.parseInt(xmlList[0]);
Log.d("test", "1st string is: " + first);
P.S: I haven't tested the code but I hope you got the logic. All the best. Hope it helps.
Use your TypedArray like this:
TypedArray mTypedArray = getResources().getTypedArray(R.array.xml_data);
Then, retrieve your resource ID's like this:
int id = mTypedArray.getResourceId(position, defaultResourceId);
When I used this, it was an array of String IDs, so I followed that with this:
getString(id)

How to underline the string in a string-array

I have a string-array in strings.xml. How can I underline the string in item?
For string we do like this..
<string name="clickable_string">This is a <u>clickable string</u></string>
I tried something like this.
<string-array name="products">
<item>this is a <u> clickable</u> string </item>
<item> <u> clickable</u> string </item>
<item>this is a <u> clickable</u> string </item>
</string-array>
Its not working. How can I do that?
Thank you
Try this way it will work
<string-array name="description">
<item> <Data> <![CDATA[ Check this <u>Redirect to Next Activity</u> ]]></Data> </item>
</string-array>
In java Code use this :
ArrayList<String> title_list = new ArrayList<String>();
String[] description_Array = getResources().getStringArray(R.array.description);
String categoryAndDesc = null;
for(String cad : description_Array) {
categoryAndDesc = cad;
title_list.add(categoryAndDesc);
}
CharSequence sequence = Html.fromHtml(categoryAndDesc);
seperator_view.setText(strBuilder);
seperator_view.setMovementMethod(LinkMovementMethod.getInstance());
Try this:
<string name="clickable_string">This is a <u>clickable string</u></string>
(i.e) Replace the < and > with &lt&semi; and &gt&semi; in your strings.xml for HTML formatted content.
And in your code use:
String text = context.getString(R.string.clickable_string);
myTextView.setText(Html.fromHtml(text));

Categories

Resources