Saturday, August 1, 2015

Change image when we click on buttons in Android

This is third android application and in this application, you will learn how to call a method when we click on any button and how to change image on button click. Create new project and drag image view, two button views in linear layout and give id imageview1,button1 and button2 respectively. To change image on button click, we are calling a method in Java file but we have to declare this method in XML file, use this code in button tag : android:onClick=”method_name” and use same method name in Java file and passview object in this method which will keep id of clicked button. The code of android XML file is give below:

Change image when we click on buttons in AndroidChange image when we click on buttons in Android


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#458"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="First Image "
        android:onClick="mess"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="Second Image"
        android:onClick="mess"/>
</LinearLayout>

Now open android Java file and use the same method which is declared in button tag in XML file. Use view_object.getId() to get id of clicked button and perform action according to which button is clicked. The code of android Java file is given below:


package com.smr.raza; //your package name 

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

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    //mess method is declared in XML file
    //This function will call when we click on any button
    //and we have to pass View object in this method
    //which will take id of clicked button
     
    public void mess(View v)
    {
      //initialize image view object
      ImageView im=(ImageView)findViewById(R.id.imageView1);
      //get clicked button id from view object
      switch(v.getId())
      {
      case R.id.button1:
      //if button1 is clicked than set image1
      im.setImageResource(R.drawable.myimage); 
      break;
      case R.id.button2:
      //if button2 is clicked than set image2
      im.setImageResource(R.drawable.myimage2); 
      break;
      }  
    }     
}

Now runs your project on emulator and if you have any doubt please comment.

No comments:

Post a Comment