how to load images from resources - android

How can I load images from resource folder in my GridView Adapter ?
I have a 200 images in resource folder i named them as name_1.png,name_2.png.
I want to load image and text view into android gridview element and show the images according to the number from resource folder.
I am able to show the text view data but not the images.
can any one suggest me how to do this. I am thinking that I have to write code in getview method to change the image.
This is getview method that I tried

Put your images into drawable folder and reach them by name, then you access them by name.
int resourceId = context.getResources().getIdentifier(name, "drawable",
context.getPackageName());
Then you can use the resource
imageView.setImageResource(resourceId);
or
imageView.setBackgroundResource(resourceId);

just a convenient refactoring of the accepted answer if you use kotlin and like using its extensions functions:
fun ImageView.loadDrawableFromName(name:String, ctx: Context, visible:Int=View.VISIBLE){
val resId = ctx.resources.getIdentifier(name, "drawable", ctx.packageName)
this.setImageResource(resId)
this.visibility = visible
}
you can call it in both ways:
imgname="IMGNAME"
myimageview.loadDrawableFromName(imgname, ctx)
and
imgname="IMGNAME"
myimageview.loadDrawableFromName("#drawable/$imgname", ctx)

Related

Refer to id of views dynamically with template strings?

(I'm sorry if I didn't use the correct term in the title.)
I have many widgets of ImageView with ids of image1, image2, image3, etc.
I would like to refer to one of them (for example image2) dynamically using an integer, something like:
val position = 2
image{position}.x = ....
Is such thing possible in Kotlin/Android? I'm only trying this direction since I know it's possible in JavaScript
You can use getIdentifier to get the identifier of your images like so:
int resId = context.resources.getIdentifier(
"image${position}",
"drawable",
context.packageName
);
or to get ids of image views like so:
int resId = context.resources.getIdentifier(
"image${position}",
"id",
context.packageName
);
Once you have the id's you could load drawables or views how you normally would with findViewById(resId) or ContextCompat.getDrawable(context, resId)
Make sure to call findViewById() on the parent of the view you are trying to find.

Looking for reasons why Bitmap.decodeResource using resourceId would return null

In my GridView adapter I'm trying to efficiently load images so I've been using Picasso 2.5.2 (which allows for loading by resource id).
Picasso.with(mContext)
.load(resId)
.noFade()
.resize(newSize, newSize)
.into(imageView);
However, it always gives a blank image when I pass in a resourceId with this message.
SkImageDecoder::Factory returned null
Before I checked the Picasso Github page I thought I would try it with using the Framework call:
imageView.setImageBitmap(BitmapFactory.decodeResource(App.get().getResources(), resId));
App.get() is return my app singleton so the Context is legit. I have also tried passing in the context from the parent in getView params and passing in an Activity Context. Logging the resId shows me the correct id for the drawable and it still doesn't work, however this works:
imageView.setImageResource(resId);
...but it's no good because I it doesn't give me the chance to reserve memory by loading the image in a smaller size.
I've tried on both Genymotion and a real device with the same results.
So both the context and resource id are fine in Logcat, what else could be the issue here?
Resource id is the integer value of the drawable.Try following code to get resource id:
int resId = resources.getIdentifier(
productImageName, "drawable", context.getPackageName());
You can learn more from here.

Can't dynamically change ImageView by code

I would like to understand, why I can't dynamically change the drawable resource from my imageView.
I have different use cases, and in function of these use cases I have to change my imageView. In my code I tried these solutions below, but I can't get my imageView refresh. It keeps the default drawable resource defined first. Here what I tried:
int imgRes = activity.getResources().getIdentifier("packageName:drawable/"+"my_drawable_name", null, null);
OR
int imgRes = activity.getResources().getIdentifier("drawable/"+"my_drawable_name", "drawable", activity.getPackageName());
imageIcon.setImageResource(imgRes);
imageIcon.invalidate();
And also:
imageIcon.setImageDrawable(null);
imageIcon.setImageDrawable(activity.getResources().getDrawable(R.drawable.my_drawable_name));
imageIcon.invalidate();
I'm in one adapter and I passed this ImageView from my activity to my adapter. And I'm doing this operation from this adapter which go the instance of my imageView.
So I tried to change this resource my_drawable_name but in both cases above, my imageView is never updated/refresh, and its image resource doesn't change. I have no error it just doesn't work. Am I doing something wrong ? What's the best practice to dynamically change a resource from an imageview by code?
No need to append drawable directory. It is the default behavior of android to get best suited drawable according to device.
Try using following code:
int imgRes = activity.getResources().getIdentifier("my_drawable_name", "drawable", activity.getPackageName());
try imageIcon.setImageResource(R.drawable.my_drawable_name);

How to set ImageView using the name String of the local resource avoiding conditional mapping of resources

I want to set an ImageView programatically, passing the name of the local resource drawable as String.
The drawable clearly are int identified in this way
R.drawable.mydrawable_name
How could I do to solve this problem without map everything in a conditional switch?
I want to avoid something in this form
if(myString.equal"stringname_1"){
myImageview.setImageResource( R.drawable.stringname_1);
}
else if(myString.equal"stringname_1")....
etc
You can use getResources().getIdentifier() (http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier(java.lang.String, java.lang.String, java.lang.String)) for this purpose. For your code it would look something like this:
int id = getResources().getIdentifier(myString, "drawable", getPackageName());
Drawable drawable = getResources().getDrawable(id);
myImageview.setImageDrawable(drawable);
The code above assumes you're in an Activity.
You can do it using java reflection
Field field = R.drawable.class.getDeclaredField("mydrawable_name");
int a = field.getInt(this);
a will have the id value of R.drawable.mydrawable_name . Now you can use this id to set image
Pl Note: Below one is need to be comment for the accepted answer but I dont have previlage to add comment.
getDrawable(id) is deprecated now instead we can use
getDrawable(int id, Resources.Theme theme)
Return a drawable object associated with a particular resource ID and styled for the specified theme.
getTheme return the current theme.
Sample is given below:
Drawable drawable = resources.getDrawable(id, context.getTheme());

Dynamic Resource Loading Android

I'm trying to find a way to open resources whose name is determined at runtime only.
More specifically, I want to have a XML that references a bunch of other XML files in the application apk. For the purpose of explaining, let's say the main XML is main.xml and the other XML are file1.xml, file2.xml and fileX.xml. What I want is to read main.xml, extract the name of the XML I want (fileX.xml), for example, and then read fileX.xml. The problem I face is that what I extract form main.xml is a string and I can't find a way to change that to R.raw.nameOfTheFile.
Anybody has an idea?
I don't want to:
regroup everything in one huge XML file
hardcode main.xml in a huge switch case that links a number/string to the resource ID
I haven't used it with raw files or xml layout files, but for drawables I use this:
getResources().getIdentifier("fileX", "drawable","com.yourapppackage.www");
to get the identifier (R.id) of the resource. You would need to replace drawable with something else, maybe raw or layout (untested).
I wrote this handy little helper method to encapsulate this:
public static String getResourceString(String name, Context context) {
int nameResourceID = context.getResources().getIdentifier(name, "string", context.getApplicationInfo().packageName);
if (nameResourceID == 0) {
throw new IllegalArgumentException("No resource string found with name " + name);
} else {
return context.getString(nameResourceID);
}
}
There is another method:
int drawableId = R.drawable.class.getField("file1").getInt(null);
According to this blog it's 5x times faster than using getIdentifier.

Categories

Resources