I want to change the text on some buttons dynamically.
All necessary text is saved at the strings.xml and the reference name is saved in a Json Object.
Anyone knows how i can get the data from strings.xml for my setText function?
JSONObject e = Options.getJSONObject(i);
//The Name for strings.xml
String name = e.getString("name");
Resources res = getResources();
button1.setText(String.format(res.getString(R.string. ??????????? )));
Use something like this
getResources().getIdentifier(name, "string", “com.main.package”));
or better:
getResources().getIdentifier(name, "string", getPackageName()));
You can get the any string from String.xml by
getString(R.string.string_name);
Related
I have a image in res folder like this
I want to access this one dynamically like this
holder.Viewcover.setImageDrawable(Drawable.createFromPath("R.id." + CoverimgUrl.get(position)));
CoverimgUrl is a list which have two image name one is book_cover & and another is blank_image this array is generated dynamically so how can I set this image from that list
In One word how to access a image dynamically which is in drawable folder and I need get that image name from an array list ?
Resources res = getResources();
String mDrawableName = "image_name";
int resourceId = res.getIdentifier(mDrawableName , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resourceId);
icon.setImageDrawable(drawable );
First Make CoverimgUrl list of integer
List<Integer> CoverimgUrl =new ArrayList<Integer>();
CoverimgUrl.add(R.drawable.book_cover);
CoverimgUrl.add(R.drawable.blank_image);
Then
holder.Viewcover.setImageResource(CoverimgUrl.get(position));
createFromPath expects a path to the file, not it's ID.
You can use the following:
int id = getResources().getIdentifier(CoverimgUrl.get(position), "id", getPackageName());
holder.Viewcover.setImageDrawable(getResources().getDrawable(id));
getIdentifier() gets the ID from the string. When you use the "R" class, it contains static integers for ids. So R.id.some_name is actually an integer, which is the id of the some_name resource.
once you get this integer with getIdentifier, you can use getResources().getDrawable() to get the drawable with the given ID.
Let me know if this works.
i have string like #string/screen_enable_auto_mode. I know that i can use:
String mess = getResources().getString(R.string.screen_enable_auto_mode);
and i will get a message.
But i have whole string as #string/screen_enable_auto_mode. How can i parse it to
String mess = getResources().getString(R.string.screen_enable_auto_mode);
that i will get a message from xml?
You can use Resources.getIdentifier for this.
int resId = getResources().getIdentifier(
"screen_enable_auto_mode", "string", "com.package.app");
String mess = getResources().getString(resId);
Replace "com.package.app" with the actual package of your app, of course.
i have this question about get string values without use an string-array on xml, i just don't know if its possible.
My file.xml can have both this
<resources>
<string name="test1"></string>
<string name="test2"></string>
</resources>
and this:
<resources>
<string name="test1"></string>
</resources>
Is there any way to get the values programatically, without an array.
The problem is, i can't do this:
R.string.test1;
R.string.test2;
Because not always i have the "test2" string. Is there any way to get all the values dinamically?
Thanks.
You could receive the string with its name (instead of its id):
String packageName = getPackageName();
int resId = getResources().getIdentifier("test2", "string", packageName);
String test2 =getString(resId);
Try this
String test1 = getResources().getString(R.string.test1);
String test2 = getResources().getString(R.string.test2);
more info
I need to randomly select a string defined within strings.xml file in android.
For example my strings.xml is :
<resources>
<string name="str1">Content comes here1</string>
<string name="str2">Content comes here2</string>
<string name="str3">Content comes here3</string>
</resources>
Can I randomly select one of these strings in my Activity?
Create an array contains all of your resource names you want to select:
String[] strs = new String[] {"str1", "str2", "str3"};
Get a random index:
int randomIndex = new Random().nextInt(3);
Get your random string from resource:
int resId = getResources().getIdentifier(strs[randomIndex ], "string", your_package_name);
String randomString = getString(resId);
The best way is you declare you Strings as an Array, then get it like this:
String[] arrayOfStrings = context.getResources().getStringArray(R.array.your_string_array);
String randomString = arrayOfStrings[new Random().nextInt(arrayOfStrings.length)];
Then you can use it as you like.
You would probable rather make it an array of strings (and then that is easier to select at random one of the array). Else, you can put the ids of your strings in an array and randomly select one of the items in the array.
I have a resources file called string.xml in res/valus directory
the file has many string resources
how can I obtain these items in an array from code ?
I tried
Field[] x=R.string.class.getFields();
but it does not work.
thanks
It works fine for me.
You might detail your problem a little more though - what doesn't work?
Note that the R.string class contains ints, not strings. Are you expecting strings? If you want a string from that resourceId int, call Context.getString(resourceId).
This is simple example of enumaration strings.You can use it.
Field[] fields = R.string.class.getFields();
for(final Field field : fields) {
String name = field.getName(); //name of string
try{
int id = field.getInt(R.string.class); //id of string
}catch (Exception ex) {
//do smth
}
}