Android ImageView example
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.android3d);
See https://www.mkyong.com/android/android-imageview-example/
For more details
1. Add Image to Resources
Put your images into folder “
res/drawable-ldpi
“, “res/drawable-mdpi
” or “res/drawable-hdpi
“.
See figure below, no matter which folder you put, Android will find your image automatically. In this case, both “android.png” and “android3d.png” images are used for demonstration.
2. Add ImageView
Open “res/layout/main.xml” file, just add an
ImageView
and Button
for demonstration. By default, imageView1
will display “android.png”.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android" />
<Button
android:id="@+id/btnChangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image" />
</LinearLayout>
2. Click on the button, image will changed to “android3d.png”.
Download Source Code
Download it – Android-ImageView-Example.zip (57 KB)