2

EDIT. I try to load variable image but somehow its not working. I think i have wrong xml imageview setup.

ImageView lblPic = new ImageView(this);
int resID = getResources().getIdentifier(pic, "drawable", getPackageName());
lblPic.setImageResource(resID);

xml imageview Using xml like this i hoped that code in Java will replace android:src defined image but it doesnt. If im not using android:src it cause error. Any suggestions?

1 Answer 1

4

If you have the name of the pictures, you need to do

int resID = getResources().getIdentifier(filename, "drawable", getPackageName());

where filename is the part that comes AFTER R.drawable., and then call

lblPic.setImageResource(resId);
Sign up to request clarification or add additional context in comments.

2 Comments

Do i have to make imageview in xml file or it can work without it?
you can create the imageview in code if you need to. i prefer to do it in xml. it keeps your code cleaner and it is easier to set some properties in xml than in code. use (ImageView) findViewById(R.id.imageViewIdFromXML) to get the imageview if you define it in xml

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.