I have this code where I want to set an image from a string:
String imagename = "mypicture";
ImageView lblPic = new ImageView(this);
int resID = getResources().getIdentifier(imagename, "drawable", getPackageName());
lblPic.setImageResource(resID);
This is my xml file.
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image1" android:src="@drawable/no_image" />
Where could there be a problem? I think I've setup xml file wrong...? I've tried many codes but every time Java code doesn't change the picture, it stays as it was in xml file.
Correct :
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image1"
android:src="@drawable/no_image" />
Place this line in onCreate Method of your Activity :
ImageView lblPic = (ImageView) findViewById(R.id.image1);
int resID = getResources().getIdentifier(imagename, "drawable", getPackageName());
lblPic.setImageResource(resID);