Click on Button to Change the Image in ImageView in Android

First of all create New Project and give the Name And MainACtivity name all that.
 
Now open the MainActivity.
 
MainActivity 
  1. public class MainActivity extends AppCompatActivity {  
  2.     ImageView img;  
  3.     Button b;  
  4.     int flag = 0;  
  5.   
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.   
  11.         img = (ImageView) findViewById(R.id.imageView1);  
  12.         b = (Button) findViewById(R.id.button1);  
  13.   
  14.         b.setOnClickListener(new View.OnClickListener() {  
  15.   
  16.             @Override  
  17.             public void onClick(View v) {  
  18.                 // TODO Auto-generated method stub  
  19.   
  20.                 if (flag == 0) {  
  21.                     img.setImageResource(R.drawable.home1);  
  22.                     flag = 1;  
  23.                 } else if (flag == 1) {  
  24.                     img.setImageResource(R.drawable.home2);  
  25.                     flag = 2;  
  26.                 } else if (flag == 2) {  
  27.                     img.setImageResource(R.drawable.home3);  
  28.                     flag = 0;  
  29.                 }  
  30.             }  
  31.         });  
  32.     }  
  33.   
  34.     @Override  
  35.     public boolean onCreateOptionsMenu(Menu menu) {  
  36.         // Inflate the menu; this adds items to the action bar if it is present.  
  37.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  38.         return true;  
  39.     }  
  40.   
  41.     @Override  
  42.     public boolean onOptionsItemSelected(MenuItem item) {  
  43.         // Handle action bar item clicks here. The action bar will  
  44.         // automatically handle clicks on the Home/Up button, so long  
  45.         // as you specify a parent activity in AndroidManifest.xml.  
  46.         int id = item.getItemId();  
  47.   
  48.         //noinspection SimplifiableIfStatement  
  49.         if (id == R.id.action_settings) {  
  50.             return true;  
  51.         }  
  52.   
  53.         return super.onOptionsItemSelected(item);  
  54.     }  
  55. }  
Now Open Your activity_main.xml file.
 
activity_main.xml 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <TextView  
  7.         android:id="@+id/textView1"  
  8.         android:textSize="20dp"  
  9.         android:textColor="#FF0000"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_centerHorizontal="true"  
  14.         android:layout_marginTop="110dp"  
  15.         android:text="Static ImageView" />  
  16.   
  17.     <ImageView  
  18.         android:id="@+id/imageView1"  
  19.         android:layout_width="60dp"  
  20.         android:layout_height="60dp"  
  21.         android:layout_centerVertical="true"  
  22.         android:layout_centerHorizontal="true"  
  23.         android:src="@drawable/home1" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/button1"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignParentBottom="true"  
  30.         android:layout_centerHorizontal="true"  
  31.         android:layout_marginBottom="80dp"  
  32.         android:text="Change" />  
  33.   
  34. </RelativeLayout>  
In your Drawable Folder add below name Image. The Image are contain in Rar file.
 
home1.jpg
home2.jpg
home3.jpg
 
Or you can use your image by giving this Name to Image.
 
Now Run the Application the following Output is Generated.
 
 
 
Click on Button the following output is generated
 
 
 
And then,
 
 
 
The Image is in the Drawable file 
Ebook Download
View all
Learn
View all