How to set image in imageview in android?

you can directly give the Image name in your setimage as iv.setImageResource(R.drawable.apple); that should be it.


Instead of setting drawable resource through code in your activity class you can also set in XML layout:

Code is as follows:

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/apple" />

use the following code,

    iv.setImageResource(getResources().getIdentifier("apple", "drawable", getPackageName()));