Search Tutorials

Loading...

Sunday, 15 September 2013

Change image using a single button in Android


This is fourth android application and this is same as third application but using only one button to change image here, so see third application and compare. Create new project and drag image view, one button view in relative layout and give id iv and bt 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 pass view object in this method which will keep id of clicked button. The code of android xml file is give below:


<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"
   android:background="#b43" >
<ImageView
   android:id="@+id/iv"
   android:layout_width="200dp"
   android:layout_height="200dp"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   android:layout_marginTop="72dp"
/>
<Button
   android:id="@+id/bt"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/iv"
   android:layout_centerHorizontal="true"
   android:onClick="change_image"
   android:textSize="20sp"
   android:text="Change image" />
</RelativeLayout>

Now open android Java file and use the same method which is declared in button tag in XML file. Use a Boolean flag to change the image. The code of android Java file is given below:


package com.example.checkblogapp; //your package name

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

public class MainActivity extends Activity {
  Boolean flag=false;
  @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 button
  //and we have to pass View object in this method
  //which will take id of clicked button

  public void change_image(View v)
  {
    ImageView iv=(ImageView)findViewById(R.id.iv);
    //use flag to change image
    if(flag==false)
  {
      iv.setImageResource(R.drawable.myimage);
      flag=true;
    }
    else
    {
      iv.setImageResource(R.drawable.myimage2);
      flag=false;
    }
  }
}

Comment to improve this code and share your ideas to make it better...

Related Tutorials:- 

Take numbers from EditText and add them and display on TextView

Make Temperature conversion application

Create Menu using XML

Create Menu using Java

Access Call, Camera and Web pages

13 comments:

  1. how i have to code if i want to show more than 2 images for eg supposed to be 5 images and should start with first image at the end of 5 th image..

    ReplyDelete
    Replies
    1. simple use it:

      int i=0;
      if(i==0)
      {
      iv.setImageResource(R.drawable.myimage);
      i++;
      }
      else if(i==1)
      iv.setImageResource(R.drawable.myimage1);
      i++;
      }
      ....
      ....
      ....
      else if(i==4)
      {
      iv.setImageResource(R.drawable.myimage4);
      i=0;
      }

      Delete
    2. i like your tuturials very much, it keeps simple and clearly.Absolutely,you must be the believer of "talk is cheap,show me the code".Thank you for make all those great guides and replies!

      Delete
    3. Thanks a lot. :)

      Delete
    4. above code not working...give alternative...not dis if...else

      Delete
    5. Bro above code is working...check your code.

      Delete
    6. i want change image
      suppose that i m taking weight sum2 if i click 1 button than change second button

      Delete
    7. thanks a lot , this is a great help to me

      - IT Student

      Delete
  2. sir, i have to play slide show while the activity is created.. but i don't know how to code for it sir

    ReplyDelete
  3. How can i add a button for going back to a previous image?

    ReplyDelete
    Replies
    1. you can add a button and make a explicit intent code..so that u could navigate from current activity to previous activity

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Using toggle button

    ur main java file

    public void change_image(View v)
    {
    ImageView iv=(ImageView)findViewById(R.id.iv);
    if(flag==false) {
    iv.setImageResource(R.drawable.defand);
    flag = true;

    }else
    {
    iv.setImageResource(R.drawable.images);
    flag = false;

    }
    }


    in ur xml file

    ReplyDelete

Back to Top