Android Widget – ImageView

android image viewThe ImageView class can load images from various sources (such as resources or content providers), takes care of computing its
measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

Add ImageView

Open “your.xml” file, just add an ImageView and Button for demonstration. By default, imageView1 will display “yourImage.png”.

Creating a ImageView in a Layout File
<!--?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/yourImage">
    <button android:id="@+id/btnChangeImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Change Image">
    
</button></imageview></linearlayout>

Simple, when button is clicked, change it to “your_another_image.png”.

public class MainActivity extends Activity {
    Button button;
    ImageView image;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        addListenerOnButton();
    }
    public void addListenerOnButton() {
        image = (ImageView) findViewById(R.id.imageView1);
        button = (Button) findViewById(R.id.btnChangeImage);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                image.setImageResource(R.drawable.your_another_image);
            }
        });
    }