Can't dynamically change ImageView by code - android

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);

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.

How to assign drawable to ImageView in acitvity using Kotlin as language

I want to set and reset the Drawable resource of ImageView from activity, but the IDE is showing cannot assign a value to Val.
val is not possible to change. You can set image in image by directly calling the ImageView in your onCreate like this:
Imageviewid.background = resources.getDrawable(R.drawable.imageid)
Alternatively, you can to change your val to a var
Try by using the following code:
image_view.setImageResource(R.drawable.my_drawable_file)
For more information please refer the following link:
https://android--code.blogspot.com/2018/03/android-kotlin-imageview-set-image.html

how to load images from resources

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)

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());

How to get Android resource ID from dynamically-created (in Java) layer-list / LayerDrawable?

The "solution #2 (dynamic)" in this question/answer post:
overlay two images in android to set an imageview
is very close to what I want to do, which is to dynamically create a layer-list (for a status bar notification icon, I want to build-up my icon in layers), but the icon assignment in the notification API requires a resource ID (which I want to call from a service).
I cannot figure-out how to build a dynamically build a layer-list without building hundreds of layer-list .xml files (for the various combinations of icons that I would like to be able to display). Daniel's "solution #1" works wonderfully for the static .xml files, but I'm looking for a more elegant, dynamic solution.
In the above post, the code snippet:
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = r.getDrawable(R.drawable.t);
layers[1] = r.getDrawable(R.drawable.tt);
LayerDrawable layerDrawable = new LayerDrawable(layers);
appears to be what I want, but I don't know or understand how to "assign" the new layerDrawable to my notification icon (which takes a resource ID).
Thanks to all...stackoverflow is a wonderful resource!
Use "getIdentifier" to get it.Suppose that I have a file bug.png in the "/res/drawable/", so i get its ResourceID with the following code:
int resID = getResources().getIdentifier("org.anddev.android.testproject:drawable/bug", null, null);
// or
int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");
reference:
anddev.org
There is no such thing as an ID for a Drawable created at runtime. Those IDs refer to int fields in the R class, automatically created from the xml files.
Since the LayerDrawable constructor requires just a Drawable array, you can provide those Drawables made from any method. An example, would be the static method Drawable.createFromStream(InputStream is, String srcName).
http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromStream%28java.io.InputStream,%20java.lang.String%29

Categories

Resources