How to Set the Image from Drawable Dynamically in Android

How to set the image from drawable dynamically in android?

Try this:

String uri = "@drawable/myresource";  // where myresource (without the extension) is the file

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

Using setImageDrawable dynamically to set image in an ImageView

Try this,

int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);

This will return the id of the drawable you want to access...
then you can set the image in the imageview by doing the following

imageview.setImageResource(id);

How can i get images dynamically from drawable folder?

This solution feels a little bit hacky not only because it uses reflection but also because it relies heavily on being able to recognize your own drawable resources by matching their names against a certain pattern (so you should try to use really unique names for the pictures)

That being said, you can change your code as follows:

Keep the private int[] image; but initialize it in the Adapter's constructor:

SwipeAdapter(Context cx){
this.cx=cx;
buildImageArray();
}

with a new method (note: you have to import java.lang.reflect.Field;)

private void buildImageArray() {

// this will give you **all** drawables, including those from e.g. the support libraries!
Field[] drawables = R.drawable.class.getDeclaredFields();
SparseIntArray temp = new SparseIntArray();
int index = 0;
for (Field f : drawables) {
try {
System.out.println("R.drawable." + f.getName());
// check the drawable is "yours" by comparing the name to your name pattern
// this is the point where some unwanted drawable may slip in,
// so you should spend some effort on the naming/ matching of your pictures
if (f.getName().startsWith("image")) {
System.out.println("R.drawable." + f.getName() + "==========================================");
int id = cx.getResources().getIdentifier(f.getName(), "drawable", cx.getPackageName());
temp.append(index, id);
index++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
image = new int[index];
for (int i = 0; i < index; i++) {
image[i] = temp.valueAt(i);
}
}

Set the image dynamically in imageView in android

I found the problem.

It must be: if ( position.equals( "0" ) ) In addition i am using only: imageView.setImageResource( R.drawable.stone_a1 )

Setting an ImageView from the drawable folder dynamicly

Solution:

Just use this code in java to set the drawable image to ImageView dynamically:

your_image_view_object.setImageDrawable(ContextCompat.getDrawable(Your_Activity.this, R.drawable.your_drawable_image));

For Example:

imageView.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_profilepic));

That's it. Hope it helps.

how to get images dynamically from drawable folder

You can use getIdentifier()

for (int j = 1; j < 6; j++) {
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier("d002_p00"+j, "drawable", getPackageName()));
}


Related Topics



Leave a reply



Submit