String in findViewById

Asked

Viewed 72 times

0

Hi, I was wondering if you could use a string in findViewById. I have an app with various imageview that when you need to call an Internet to the camera phone. So that I don’t need to put this Input and your startActivityforResult for each imageview, I thought of creating a string to change the id of each imageview according to the selected imageview. Ex:

 image=(ImageView)findViewById(R.id(aqui colocar o código de uma string);
 image.setImageBitmap(imageCamera);

Is it possible? Thank you

1 answer

1


I suggest you do it using a HashMap, since it wants to have control via String.

Would something like this:

  Map<String, Integer> map = new HashMap<String, Integer>();

    map.put("Image1",R.id.imageView1);
    map.put("Image2",R.id.imageView2);
    map.put("Image3",R.id.imageView3);

To recover the id, to your findView, you would do something like this:

 image=(ImageView)findViewById(map.get("Image1"));
  • Thank you, it worked perfectly

  • Cool! Glad I helped. Abs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.