I have a list of Drawable stored in a Array. Now I would like to convert it into "res"(int datatype). Is this possible? Any help is appreciated.
It is not possible to modify the resources in an existing APK on the device.
You're probably trying to get a resID from a random Drawable. If so then this is impossible - drawable doesn't store IDs.
But if you use TypedArray to load your drawables from resources (i.e. Resources.obtainTypedArray(R.array.your_imgs)) then you'll be able to get resID by calling getResourceId(int index, int defValue):
TypedArray typedArray = mResources.obtainTypedArray(R.array.my_imgs);
int resID = typedArray.getResourceId(index, defValue);
"res" is an identifier to access a resource packed in .apk file. So I think to "convert" drawable to res actually means modifing existing .apk on device, which is impossible
if you have the drawable names in the array you can use
getIdentifier (String name, String defType, String defPackage);
int resId = getResources().getIdentifier("us","drawable","com.app");
The above function will return an integer value same as R.drawable.us.
This is how you access with resource names.
Related
I am trying to get Id of image resource in drawable folder.
Here is my code.
...
int resourceId = Context.Resources.GetIdentifier("Elements_ok", "drawable", this.context.PackageName);
I always got the result of Zero.
But I got Id of image named error.png.
int resourceId = Context.Resources.GetIdentifier("error", "drawable", this.context.PackageName);
Is there a way that I can fix it?
Yep, just change your drawable filenames to lowercase.
And, of course, update any code that uses those filenames accordingly to use lowercase strings.
Since Android only supports lowercase filenames for resource items, I would also recommend updating all your drawable resource filenames to lowercase to eliminate any other potential issues.
the problem is that I have various XML files with the data that I need for my app and I want to access to read them by the name of the XML file: "fileName.xml" so I can run the pull parser. Since the program only knows which file to load by its name which is given by a string I can't call them using r.id.fileName.xml.
I have tried things like:
int resID = getResources().getIdentifier("r.id.fileName", "id", getPackageName());
and then call it by this resID... but it doesn't seem to work (resID is always 0) :(
Any ideas?
Thank you!
The best solution from a performance standpoint is to have a mapping in your Java code between some string and the resource ID value:
private static HashMap<String, Integer> RESOURCES=new HashMap<>();
static {
RESOURCES.put("thingy", R.xml.thingy);
RESOURCES.put("thingy2", R.xml.thingy2);
// and so on
}
Then, you can just call RESOURCES.get() to retrieve the ID value, without having to use the reflection that goes on inside of getIdentifier().
If you really want to use getIdentifier(), then the syntax would be:
int resID = getResources().getIdentifier("fileName", "xml", getPackageName());
because:
the first parameter is the base name of the resource
the second parameter is the type of the resource, and the files in res/xml/ are not id resources
In Android, to access a string from the strings.xml file, we use R.string.string_id. Would it be possible to have a method such that we'll use the string form of string_id? I mean can we for example have a method GetString("string_id") to retrieve R.string.string_id?
//Replace this with appropriate context
String name = "name_of_your_string_in_strings_xml_file_goes_here";
int resId = this.getResources().getIdentifier(name, "string", this.getPackageName());
String string = this.getResources().getString(resId);
It is possible. You have to understand the fact that any data that is pinned to xml files in android are considered as resource to the app. And android resource can be accessed only by means of unique id we provide to them. These ids that are generated are of type int and hence we need to pass a int parameter to get a reference to these resource at run time.
But it is not that only the default way to access them is by using their res value but instead if you could know the name of the string key you could find the id from there and then use the int id to do this.
I have an android application that use a gallery component to choose an icon and assign it to a button.
The icon set is located in res/drawable folder and is accessed in the gallery with the typical adapter of the guide:
private Integer[] Imgid = {
R.drawable.icon_home,
R.drawable.icon_home2,
...
}
After an icon choosing, i stored the settings in a db with id of the button and of the drawable.
All works done, but i've noticed that if i'll want to add or modify my icon set or resources in general of my application, ids of old resource could change, so the preferences in the db refers to wrong icon.
Is there a way to force the old id of R class so they didn't change in the future? Or is there another way to manage the item of the component galley with another attribute than id? (maybe string name and reflection?)
Thanks in advance
You can store the name of the drawable in the database if you don't plan to change that. getResourceEntryName returns the name from the resource id:
Resources resources = getResources();
String name = resources.getResourceEntryName(R.drawable.icon);
And you can get back the resource id from the name with getIdentifier:
int resId = resources.getIdentifier(name, "drawable", "your.package.name");
You can't use static int for resource identifier, however you should look at two methods od Resources class:
getIdentifier()
getresourceName()
You shouldn’t rely on the actual values of the R.drawable.* attributes.
Create your own ids instead (for example 1 correspond to R.drawable.icon_home and 2 correspond to R.drawable.icon_home2)
Edit:
String name and reflection should work too, but it’s probably a little overkill you have only a few icons.
I have an array contains string values, in that same name i have image in drawable.so i want to add in integer array how to do that? ex string newarray[]={"jj","kk","ll"};
i want to access R.drawable.jj ,R.drwable.kk,R.drawable.ll
To access the drawables you need an int. Instead of saving the names in a String array, save the IDs in an int array:
int[] drawables = {R.drawable.jj, R.drawable.kk, R.drawable.ll};
Then all you need to do to access them is call:
getResources().getDrawable(drawables[0]);
You could try to get the identifier by name:
Resources res = getResources();
res.getDrawable(res.getIdentifier(myStringArray[0], "drawable", myPackage));
A similar question was already answered: get resource id by passing name as a parameter in android