0

Sorry for the title, had troubles describing what I'm trying to do. I've always had this question for Java as a whole, but right now, its specific for Android. Let's say that I am parsing an API and get this special String, let's say "clear". Inside my drawables, I have a clear.jpg that I would like to set as an ImageView programically . That's not the hard part. What I was wondering is if there was a quick way where I Could call on the .jpg with just the variable name? I know I can easily make different if statements, such as:

if(string == "clear")
{
    //setImageView to drawable/clear
}

but is there anyway I can do something such as

//setImageView to drawable/string

where string would be clear? I know obviously it would look for a string inside drawable, but is there anyway I could do what I'm describing? Just a general question I had; it was something I always wondered about.

Let me know! Thanks!

hata
  • 11,633
  • 6
  • 46
  • 69
user2397837
  • 43
  • 1
  • 9
  • You can't do `string == clear`. Well you can, but I doubt it's what you're trying to do. See this: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Zarwan Sep 16 '15 at 21:09

5 Answers5

0
private void showImage() {
    String uri = "drawable/icon";

    // int imageResource = R.drawable.icon;
    int imageResource = getResources().getIdentifier(uri, null, getPackageName());

    ImageView imageView = (ImageView) findViewById(R.id.myImageView);
    Drawable image = getResources().getDrawable(imageResource);
    imageView.setImageDrawable(image);
}

Else I would recommend you to work with R.* references like this:

int imageResource = R.drawable.icon;
  Drawable image = getResources().getDrawable(imageResource);

Reference -> Android - Open resource from @drawable String

Community
  • 1
  • 1
Techidiot
  • 1,921
  • 1
  • 15
  • 28
0

With what i know, the quickest way to call up that picture is putting the

picture's name in the xml file; For instance if you want to change the background

of an imageview, you can just put in your xml file this code below

android:background="@drawable/clear".

Note the image must have been in the drawable folder. Thanks, i hope this helps.

james jelo4kul
  • 839
  • 4
  • 17
0

It looks like Java 8 provides limited support for local variable annotations, but its reflection API can't give you access within method bodies. So you'd need to parse the class file itself. And for that, a library like ASM would do the trick.

Keith
  • 3,079
  • 2
  • 17
  • 26
0
if(string.equals("clear")){
  int resID = getResources().getIdentifier("clear", "drawable", getPackageName());
  imageView.setBackgroundResource(resID);
}
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
0

Same as other ones at the point which is using getIdentifier method.

imageView.setImageView(getResources().getIdentifier(string, "drawable", 
            getPackageName()));

However, by doing this, you can process without if-condition block and just specify a Drawable of the value from string directly (I think it is your intention of this question).

hata
  • 11,633
  • 6
  • 46
  • 69