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
Related
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);
I have written a program in android. In it I have added all my images in the drawable folder eg dress_1.png, dress_2. But image name dress_1 is added into the string-array resource. Now I don't have idea how to retrieve the images from this string-array resource and set as background into the imageview. I have mentioned following code. Please, help me to complete my task. I will really appricate you.Thank you in advance.
Summary: How to retrieve string value from an array-string into java and how to use retrieved string images from the drawable folder in android
xml file (small code)
<ImageView
android:id="#+id/T1C4R1"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight=".45"
android:background="#drawable/rszjumper"
android:contentDescription="#string/desc" />
SecondActivity.java
for (int c = 0; c < conditions.length; c++) {
weatherCondition(conditions[c]);
}
private void weatherCondition(int c) {
switch(c){
case 5:
String dress_array[] = getResources().getStringArray(R.array.five);
weatherDress(dress_array);
}
}
private void weatherDress(String[] dress_array) {
for(int d =0; d < dress_array.length; d++ ){
String img = "drawable/" + dress_array[d];
T1C4R1.setBackground(img);
//img.setImageResource(getResources().getIdentifier(image_string[1] , "drawable", getPackageName()));
}
}
string.xml
<!-- condtion 5 -->
<string-array name="five" >
<item name="dress_2">dress_2</item>
<item name="dress_8">dress_8</item>
<item name="dress_6">dress_6</item>
</string-array>
Normally you would look up the drawable based on its resource ID (R.drawable.dress_1, for example). If you're looking them up based on a string, you need to first find the resource ID based on the name.
public Drawable getDrawableByName(Context context, String name)
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(name, "drawable",
context.getPackageName());
return resources.getDrawable(resourceId);
}
T1C4R1.setImageDrawable(getDrawableByName(getContext(), "dress_2"));
Alternatively, you could simply return the resource ID and use setImageResource to assign the drawable based on its ID.
You should also note that your current structure will only display the last image in the array, since you are overwriting the drawable image with each pass in your loop.
I have a preference xml file and a listpreference.
The listpreference entryValues and entries are in a array.xml file.
Here's the problem, the entries/entryValues contain "10 mb/s" and i would like to get the int value from that entry/whatever is selected.
This gives me an error however, here is the code:
TextView t = (TextView)findViewById(R.id.textView1);
String result = sp.getString("BITRATE", "8");
int i = Integer.parseInt(result.substring(result.lastIndexOf(" mb/s")));
t.setText("Int value: " + i);
As i said this gives me an error and i cannot find the issue.
Thanks for any help!
FINAL FIXED CODE
TextView t = (TextView)findViewById(R.id.textView1);
String result = sp.getString("BITRATE", "3");
int intRes = Integer.parseInt(result);
t.setText("Int value: " + intRes);
Also i changed the preferences entryValues items to only be integers instead of ex 10mb/s to just 10.
Make your entryValues array containing only the integers. The entries are used when displaying to the user, the entryValues are what is stored in your preferences (and are not displayed). The two arrays do not have to have the same contents, but they should have the same number of items.
<string-array name="bitrate_entries">
<item>10 mb/s</item>
<item>25 mb/s</item>
<item>50 mb/s</item>
</string-array>
<string-array name="bitrate_entry_values">
<item>10</item>
<item>25</item>
<item>50</item>
</string-array>
Now you only need to use Integer.parseInt()
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...)
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)