Introduction

In this article, you will learn how to set an image in an Image View on a click.
Step 1
First I used many Image Views and ScrollViews for the sliding activity. The first Image View sets the image on a button click and the others contain the images to be set. I saved the images in the drawable folder by copy and paste.
Step 2
Open the layout file activity_main.xml and write this:
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <LinearLayout
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:orientation="vertical"
  7. android:background="#80000000">
  8. <ImageView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:id="@+id/imageView1"
  12. android:layout_gravity="center"/>
  13. <Button
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="New Button"
  17. android:id="@+id/button" android:layout_gravity="center"
  18. android:visibility="invisible"/>
  19. <HorizontalScrollView
  20. android:layout_width="200dp"
  21. android:layout_height="wrap_content"
  22. android:id="@+id/scrollView"
  23. android:layout_gravity="center">
  24. <LinearLayout
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. android:orientation="horizontal">
  28. <ImageView
  29. android:src="@drawable/image2"
  30. android:layout_width="125dp"
  31. android:layout_height="fill_parent"
  32. android:id="@+id/imageView2"
  33. android:padding="15dp"/>
  34. <ImageView
  35. android:src="@drawable/image3"
  36. android:layout_width="125dp"
  37. android:layout_height="125dp"
  38. android:id="@+id/imageView3"
  39. android:layout_gravity="center"
  40. android:padding="15dp"/>
  41. <ImageView
  42. android:src="@drawable/images4"
  43. android:layout_width="125dp"
  44. android:layout_height="125dp"
  45. android:id="@+id/imageView4"
  46. android:layout_gravity="center"
  47. android:padding="15dp"/>
  48. <ImageView
  49. android:src="@drawable/image5"
  50. android:layout_width="125dp"
  51. android:layout_height="125dp"
  52. android:id="@+id/imageView5"
  53. android:layout_gravity="center"
  54. android:padding="15dp"/>
  55. <ImageView
  56. android:src="@drawable/image6"
  57. android:layout_width="125dp"
  58. android:layout_height="125dp"
  59. android:id="@+id/imageView6"
  60. android:layout_gravity="center"
  61. android:padding="15dp"/>
  62. <ImageView
  63. android:src="@drawable/image7"
  64. android:layout_width="125dp"
  65. android:layout_height="125dp"
  66. android:id="@+id/imageView7"
  67. android:layout_gravity="center"
  68. android:padding="15dp"/>
  69. </LinearLayout>
  70. </HorizontalScrollView>
  71. <TextView
  72. android:layout_width="fill_parent"
  73. android:layout_height="wrap_content"
  74. android:textAppearance="?android:attr/textAppearanceMedium"
  75. android:text="Click on the Image"
  76. android:id="@+id/textView"
  77. android:layout_marginTop="50dp"
  78. android:textColor="#45454545"
  79. android:layout_marginLeft="120dp"/>
  80. </LinearLayout>
Open the Java class file Main_Activity.java and write this:
  1. package com.wallpaer;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. public class MainActivity extends Activity implements View.OnClickListener{
  12. ImageView imageView1;
  13. Button button;
  14. int tophone;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. imageView1=(ImageView)findViewById(R.id.imageView1);
  20. tophone=R.drawable.ic_launcher;
  21. ImageView imageView2=(ImageView)findViewById(R.id.imageView2);
  22. ImageView imageView3=(ImageView)findViewById(R.id.imageView3);
  23. ImageView imageView4=(ImageView)findViewById(R.id.imageView4);
  24. ImageView imageView5=(ImageView)findViewById(R.id.imageView5);
  25. ImageView imageView6=(ImageView)findViewById(R.id.imageView6);
  26. ImageView imageView7=(ImageView)findViewById(R.id.imageView7);
  27. button=(Button)findViewById(R.id.button);
  28. imageView2.setOnClickListener(this);
  29. imageView3.setOnClickListener(this);
  30. imageView4.setOnClickListener(this);
    imageView5.setOnClickListener(
    this);imageView2.setOnClickListener(this);
  31. imageView6.setOnClickListener(this);
  32. imageView7.setOnClickListener(this);
  33. button.setOnClickListener(this);
  34. }
  35. public void onClick(View v)
  36. {
  37. switch(v.getId())
  38. {
  39. case R.id.imageView2:
  40. imageView1.setImageResource(R.drawable.image2);
  41. tophone=R.drawable.image3;
  42. break;
  43. case R.id.imageView3:
  44. imageView1.setImageResource(R.drawable.image3);
  45. tophone=R.drawable.image5;
  46. break;
  47. case R.id.imageView4:
  48. imageView1.setImageResource(R.drawable.images4);
  49. tophone=R.drawable.ic_launcher;
  50. break;
  51. case R.id.imageView5:
  52. imageView1.setImageResource(R.drawable.image2);
  53. tophone=R.drawable.ic_launcher;
  54. break;
  55. case R.id.imageView6:
  56. imageView1.setImageResource(R.drawable.image6);
  57. tophone=R.drawable.ic_launcher;
  58. break;
  59. case R.id.imageView7:
  60. imageView1.setImageResource(R.drawable.image7);
  61. tophone=R.drawable.ic_launcher;
  62. break;
  63. case R.id.button:
  64. InputStream a=getResources().openRawResource(tophone);
  65. Bitmap whatever= BitmapFactory.decodeStream(a);
  66. try{
  67. getApplicationContext().setWallpaper(whatever);
  68. }
  69. catch(IOException e){
  70. e.printStackTrace();
  71. }
  72. break;
  73. }
  74. }
  75. }
Step 3
First, the User Interface will appear like this:
Clipboard02.jpg
Step 4
On the click, the image will be set on Image View and look like this:
Clipboard03.jpg
Step 5
You can change the image by sliding these images like this:
Clipboard04.jpg

Summary

In this article, we learned about How to Set Image in an Image View on Click in Android Studio.