Introduction

I explain in this article how to set a wallpaper from an ImageView in Android. In this article first of all we select an image from an ImageView using the "Select Image" Button. After that we will choose an image and we will then change the wallpaper.
All this will happen within the following procedure.
Step 1
Create a new Android project as shown below using select "File" -> "New" -> "Android Application Project".
7new.jpg
Step 2
Open "activity_main.xml" and update it as shown below.
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="wrap_content"
  4. android:layout_height="match_parent"
  5. android:layout_centerHorizontal="true"
  6. android:layout_centerVertical="true"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity" >
  9. <Button
  10. android:id="@+id/button1"
  11. android:layout_width="324dp"
  12. android:layout_height="wrap_content"
  13. android:text="@string/btn" />
  14. </LinearLayout>
Step 3
Open the "MainActivity.java" file and update it as shown below.
  1. package com.mcn.wallpaper;
  2. import java.io.IOException;
  3. import android.app.Activity;
  4. import android.app.WallpaperManager;
  5. import android.content.Intent;
  6. import android.graphics.Bitmap;
  7. import android.net.Uri;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.Toast;
  13. public class MainActivity extends Activity {
  14. Bitmap bitmap;
  15. int lastImageRef;
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. Button buttonSetWallpaper = (Button)findViewById(R.id.button1);
  22. buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
  23. //@Override
  24. public void onClick(View arg0) {
  25. Intent i = new Intent(getApplicationContext(), ImagesStorage.class);
  26. startActivity(i);
  27. }});
  28. }
  29. }
Step 4
Now create a new XML file Using "Wallpaper/res/layout/" as "image_storage.xml" as shown below.
7newactivity.jpg
Step 5
Create a new java file using "Wallpaper/src/com.mcn.wallpaper/" as "ImageStorage.java" as shown below.
7newclass.jpg
Step 6
Now open the "image_storage.xml" file and update it with the following code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:id="@+id/InerLinearLayout"
  8. android:layout_width="match_parent"
  9. android:layout_height="360dp"
  10. android:orientation="horizontal" >
  11. <ImageView
  12. android:id="@+id/imageView1"
  13. android:layout_width="108dp"
  14. android:layout_height="wrap_content"
  15. android:src="@drawable/image1" />
  16. <ImageView
  17. android:id="@+id/imageView2"
  18. android:layout_width="110dp"
  19. android:layout_height="wrap_content"
  20. android:src="@drawable/image2" />
  21. <ImageView
  22. android:id="@+id/imageView3"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:src="@drawable/image3" />
  26. </LinearLayout>
  27. </LinearLayout>
Step 7
Open the "ImageStorage.java" file and update it with the following code.
  1. package com.mcn.wallpaper;
  2. import java.io.IOException;
  3. import android.app.Activity;
  4. import android.app.WallpaperManager;
  5. import android.graphics.Bitmap;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. import android.widget.Toast;
  11. public class ImagesStorage extends Activity
  12. {
  13. Bitmap bitmap;
  14. int lastImageRef;
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.images_storage);
  19. ImageView imagePreview1 = (ImageView)findViewById(R.id.imageView1);
  20. ImageView imagePreview2 = (ImageView)findViewById(R.id.imageView2);
  21. ImageView imagePreview3 = (ImageView)findViewById(R.id.imageView3);
  22. imagePreview1.setImageResource(R.drawable.image1);
  23. imagePreview2.setImageResource(R.drawable.image2);
  24. imagePreview3.setImageResource(R.drawable.image3);
  25. imagePreview1.setOnClickListener(new ImageView.OnClickListener(){
  26. //@Override
  27. public void onClick(View arg0) {
  28. WallpaperManager myWallpaperManager
  29. = WallpaperManager.getInstance(getApplicationContext());
  30. try {
  31. myWallpaperManager.setResource(R.drawable.image1);
  32. } catch (IOException e) {
  33. // TODO Auto-generated catch block
  34. }
  35. finally
  36. {
  37. Toast.makeText(getApplicationContext(), "Thank You,\n Back Ground Changed as image 1",Toast.LENGTH_LONG).show();
  38. }
  39. }});
  40. imagePreview2.setOnClickListener(new ImageView.OnClickListener(){
  41. //@Override
  42. public void onClick(View arg0) {
  43. // TODO Auto-generated method stub
  44. //WallpaperManager mywall.getIntent()
  45. WallpaperManager myWallpaperManager
  46. = WallpaperManager.getInstance(getApplicationContext());
  47. try {
  48. myWallpaperManager.setResource(R.drawable.image2);
  49. } catch (IOException e) {
  50. // TODO Auto-generated catch block
  51. finally
  52. {
  53. Toast.makeText(getApplicationContext(), "Thank You,\n Back Ground Changed as image 2",Toast.LENGTH_LONG).show();
  54. }
  55. }});
  56. imagePreview2.setOnClickListener(new ImageView.OnClickListener(){
  57. //@Override
  58. public void onClick(View arg0) {
  59. // TODO Auto-generated method stub
  60. //WallpaperManager mywall.getIntent()
  61. WallpaperManager myWallpaperManager
  62. = WallpaperManager.getInstance(getApplicationContext());
  63. try {
  64. myWallpaperManager.setResource(R.drawable.image3);
  65. } catch (IOException e) {
  66. // TODO Auto-generated catch block
  67. }
  68. finally
  69. {
  70. Toast.makeText(getApplicationContext(), "Thank You,\n Back Ground Changed as image 3",Toast.LENGTH_LONG).show();
  71. }
  72. }});
  73. }
  74. }
Step 8
To use the "ImageView" in the "image_storage.xml" file imort or paste an image into "Wallpaper/res/drawable- xhdpi" from the yr picture collection as shown below.
7pasteimage.jpg
Step 9
Now open the "manifest.xml" file and update it with the following code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.mcn.wallpaper"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="17" />
  9. <uses-permission android:name="android.permission.SET_WALLPAPER" />
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15. <activity
  16. android:name="com.mcn.wallpaper.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. <activity android:name="com.mcn.wallpaper.ImagesStorage"></activity>
  24. </application>
  25. </manifest>
Step 10
The output of the preset screen:
7presetWall.jpg
Middle steps to changing the wallpaper:
7select image.jpg
7setImage.jpg
7selected Image.jpg
New changed screen:
7newWall.jpg