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. @Override
  6. public void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. img = (ImageView) findViewById(R.id.imageView1);
  10. b = (Button) findViewById(R.id.button1);
  11. b.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. // TODO Auto-generated method stub
  15. if (flag == 0) {
  16. img.setImageResource(R.drawable.home1);
  17. flag = 1;
  18. } else if (flag == 1) {
  19. img.setImageResource(R.drawable.home2);
  20. flag = 2;
  21. } else if (flag == 2) {
  22. img.setImageResource(R.drawable.home3);
  23. flag = 0;
  24. }
  25. }
  26. });
  27. }
  28. @Override
  29. public boolean onCreateOptionsMenu(Menu menu) {
  30. // Inflate the menu; this adds items to the action bar if it is present.
  31. getMenuInflater().inflate(R.menu.menu_main, menu);
  32. return true;
  33. }
  34. @Override
  35. public boolean onOptionsItemSelected(MenuItem item) {
  36. // Handle action bar item clicks here. The action bar will
  37. // automatically handle clicks on the Home/Up button, so long
  38. // as you specify a parent activity in AndroidManifest.xml.
  39. int id = item.getItemId();
  40. //noinspection SimplifiableIfStatement
  41. if (id == R.id.action_settings) {
  42. return true;
  43. }
  44. return super.onOptionsItemSelected(item);
  45. }
  46. }
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. <TextView
  6. android:id="@+id/textView1"
  7. android:textSize="20dp"
  8. android:textColor="#FF0000"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_alignParentTop="true"
  12. android:layout_centerHorizontal="true"
  13. android:layout_marginTop="110dp"
  14. android:text="Static ImageView" />
  15. <ImageView
  16. android:id="@+id/imageView1"
  17. android:layout_width="60dp"
  18. android:layout_height="60dp"
  19. android:layout_centerVertical="true"
  20. android:layout_centerHorizontal="true"
  21. android:src="@drawable/home1" />
  22. <Button
  23. android:id="@+id/button1"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_alignParentBottom="true"
  27. android:layout_centerHorizontal="true"
  28. android:layout_marginBottom="80dp"
  29. android:text="Change" />
  30. </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