Saturday, 3 January 2015

ImageView in Android

Used images in this project:

ScreenShots:

activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:src="@drawable/dog" />

    <Button
        android:id="@+id/button_changeImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="Change Image" />

</RelativeLayout>


MainActivity.java:
package com.ram.imageview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final ImageView imageView = (ImageView)findViewById(R.id.imageView);
        
        Button changeButton =(Button)findViewById(R.id.button_changeImage);
        changeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
 
imageView.setImageResource(R.drawable.cat);
}
});
    }
}