1

is there any possibility to set background image dynamically?!

I try to explain what I mean.

String picVariable = getPictureFromServer();

ImageView image = (ImageView)v.findViewById(R.id.dynamic_image);

// I know, that doesn't work, but that's the way I looking for
image.setBackgroundResource(picVariable); 

Thank you in advance,

Mur

Ps. I also read this article. It would suggested in one answer, to use java reflection to get a field of the R class by name. But I've never used reflextion before. An example would be very helpfull

2
  • What does getPictureFromServer return? Commented Oct 11, 2010 at 8:05
  • Something like "abc.png", and there is abc.png in drawables Commented Oct 11, 2010 at 8:15

2 Answers 2

5

Sometimes I should take a bit more time for searching :)

I found the answer reading this article, and it works fine for me:

// The server says, it should be *.png
String picName = getPictureFromServer();
picName = picName.replace(".png", "");

Resources r = getResources();
int picId = r.getIdentifier(picName, "drawable", "com.mypackage.myapp");

ImageView image = (ImageView)v.findViewById(R.id.dynamic_image);
image.setBackgroundResource(picId);
Sign up to request clarification or add additional context in comments.

Comments

0

ImageView does not have any background, but for other widget (like Button), you should use setBackgroundResource(int).

Sorry, I am not sure I read the question correctly... maybe your problem is just that you are trying to use it with a Widget that does not use background ?

1 Comment

it's not really my project, I just clean errors and add new funktionality. And there is a background for imageviews, but it seems to me very weird as well to use in an imageview a background :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.