dare
dare

Reputation: 662

how to get name of a drawable assets dynamically in android?

I have some photos in my drawable folder res that name of them is like

I have a table in my database which has a field to store photo ID.

I have a selected list of items (in my case movement), that I want, user see the proper photo based on the Id of photo.

movements = db.getAllMovements(true);
...

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  Dialog movement_image_dialog = new Dialog(getActivity());
  movement_image_dialog.setContentView(R.layout.image_dialog);

  ImageView movement_imageView = (ImageView) movement_image_dialog.findViewById(R.id.movement_imageview);

  **int photoId = finalMovements.get(position).getPhotoId();**
  **movement_imageView.setImageResource(R.drawable.*move4*);**
...
}

My question is how I can set the Image resource with the photoId that I have? I need something like this R.drawalbe.(move+photoId)

Upvotes: 0

Views: 703

Answers (3)

Krishna V
Krishna V

Reputation: 1821

we get dinamically images from like this.

String imageId=yourImageId;
    Drawable drawable = getResources().getDrawable(getResources()
                      .getIdentifier(imageId, "drawable", getPackageName()));

Upvotes: 1

gunjan luthra
gunjan luthra

Reputation: 121

I think you should try this..

int photoId=R.drawable.move1;

img.setImageResource(photoId);

Upvotes: 0

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5636

You can use resource getIdentifier(String name, String defType, String defPackage) as

int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());

Where

this is an Activity.

nameOfDrawable is move1, move2 etc in your case.

drawableResourceId is the one you can pass in

movement_imageView.setImageResource(drawableResourceId);

Upvotes: 1

Related Questions