Amit Prajapati
Amit Prajapati

Reputation: 14305

How to access Images form Drawable using Dynamic String Path

I have this type of image path

       String img_path = "R.drawable."+lower+"_25";

lower contain dynamic name of images...

this images are already in drawable how to show images in my

ImageView in android

Upvotes: 2

Views: 1366

Answers (3)

Jignesh Ansodariya
Jignesh Ansodariya

Reputation: 12685

Make constructor in your adapter

public class MyAdapter extends XXXXXXX{
Context con;
public MyAdapter(Context con) {
        this.con = con;

    }

 .
 .
 .
int id = con.getResources().getIdentifier(lower+"_25", "drawable", "<your package name>");
imageView.setBackgroundResource(image_id);

}

Upvotes: 2

Paresh Mayani
Paresh Mayani

Reputation: 128428

You can use:

getResources().getIdentifier(lower+"_25", "drawable", getPackageName())

Upvotes: 1

Nermeen
Nermeen

Reputation: 15973

You can get the id of the image in the drawable using it's name, using:

int image_id = getResources().getIdentifier(lower+"_25", "drawable", getPackageName());
imageView.setBackgroundResource(image_id);

Upvotes: 1

Related Questions