Android get resource type of id - android

I search for a way to say which resource type is behind a integer ID. For example i have the id of a drawable and another one for a color. I can load the drawable with Resource.getDrawable(id) and the color with Resource.getColor(ID) but how can i know which method is the right one for a given ID. I look for something like Resource.isColor(id)?
Has anybody a good idea. I wrote a image loader which loads images from web, filesystem and drawables but now also from hex color or color ID.

Check out this function from android Resources class. Resources.getResourceTypeName(int resid) (also this )

To find the resource type the safest solution is to use the below code
String viewName = findViewById(resourceId).toString();
String resourcetype = viewName.substring(0,viewName.indexOf("{"));
The resoucetype then contains the full classname for the resource, eg in case of EditText, it shows
android.widget.EditText
Tried some other ideas but noted that getResourceTypeName only returns 'id' as value and getTag() on View does not work in all conditions.

Related

How to change images from mipmap folder dynamically without "direct description" in code?

I have a lot of images (more than 200) with various sizes in mipmap folders, and I need to set images in ImageView dynamically... but how to do this without "direct description" in my code of that R.mipmap.*int files?
There are flag images in mipmap folders, and my object has it's own String field "flagCountry" which consists of two characters. Names of flags in mipmap folders consist of same characters. So, that's a trouble - how to dynamically set setImageResource(R.mipmap.XXX) from flagCountry String variable?
I'm thinking that decision in "reflection", but how is it possible? )) Please help
You can use Resources.getIdentifier() to find the integer id for a resource given its name. It's like reflection for android resources.
int id = getResources().getIdentifier("XXX", "drawable", getPackageName());
Or something along those lines. This assumes the code is in an activity. Then you can call setImageResource(id) with that id.
As said by Doug, you can use Resources.getIdentifier() to find the integer id for a resource given its name. But you said you wanted the resource from mipmap. So do a simple change. Change the word "drawable" to "mipmap", and you'll get the image from mipmap. The changed code:
int id = getBaseContext()getResources().getIdentifier("XXX", "mipmap", getBaseContext().getPackageName());
image.setImageResource(id);

How to add R.drawable resources to an array for android?

I would like to have sequence of set R.drawable been called in integer array so that it can be accessed later with choice.But I'll like to load to the R.drawable dynamically which corresponds to different names.It names needs to be had as per the external value input not hardcoded.I tried this in making many but like use list,set,array conversion etc.Kindly guide me with a snippet or example on this regard.Thank you.
I suppose, that best way to achieve this funcionality is by using ArrayAdapter of Integer.
You will simply add drawble resources by id.
If you need to get resource id, but you will get string input from user, you can use this code:
Android, getting resource ID from string?

Android: drawable id change after resources modify

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.

Get a ImageView value

I'm new into the "Android World" and I've a problem getting a ImageView value, suppose I've a ImageView somewhere in my layout file and I want to retrieve is value, i.e. the value in the "android:src" tag. How I can I do it?
Thanks in advance.
Once the resource file is compiled the ImageView simply refers to a resource integer in the R file. So the image "src" value will simply be a value once you've retrieved it, and not the string value you placed in your layout file.
Nonetheless, I think you can get that ID through your ImageView's getResources() method.
EDIT: I'm wrong, turns out you can get the string through Resources.getString(int id)
EDIT 2: Looks like getResources might not contain the ID for you either, an Android engineer already answered a similar question: http://groups.google.com/group/android-developers/browse_thread/thread/84d31b244163821d?pli=1

Android Bitmap syntax

I want to load bitmaps into ImageViews in Android, but I don't want to use the R.drawable syntax because I have a lot of images with a standard naming convention so it's much easier to get the image with some logic. For example, all my images are named:
img1.png
img2.png
img3.png
So if a user selects a value, let's say x, I show "img" + x + ".png" in the ImageView. Looks like Bitmap.decodeFile is what I need, but I need to know the syntax of how to get to my drawables folder since that's where the images are. Or if there's perhaps a better way.
I realize I could do this with a switch statement instead of concatenating the image name but that would be a lot of lines since I have so many images.
One alternative is to use reflection to load images from R. You can looking static variables by string name. Keep in mind that reflection can be slow though. If you are going to be loading the same image multiple times, you might want to keep a cache of name -> R id.
Edit:
You can also access them by URI. See referring to android resources using uris.
The R.drawable value is a number by itself. You just need to use the first image id and add a fixed value to find the right image. For example:
R.drawable.image1 = 1234;
R.drawable.image2 = 1235;
etc.
So to get image 2 I would to R.drawable.image1 + 1.
Every time the R.java file is regenerated the numbers may change but the sequence will be the same. If you don't want to depend in this then you will have to look at the "assets" folder.
Looks like I've found a solution. The real problem is that I generate the resource names at runtime, but I can get the resource id like this:
getResources().getIdentifier("string1" + string2, "id", "com.company.package")
I can then pass that value to setImageResource or any other method that accepts a resource id.

Categories

Resources