Setting An Image As A Wallpaper

This is a sample application to show that how to set an image as a Wallpaper.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project SetWallpaper.
2.) Add the relevant permissions to your AndroidManifest.xml file:

<!--?xml version="1.0" encoding="utf-8"?-->
      package="com.example.SetWallpaper"
      android:versionCode="1"
      android:versionName="1.0">
    
    
    

    
        
            
                
                
            
        
    

3.) Put some images in the res/drawable.
4.) Run the application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. SetWallpaper. Enter following information:
Project name: SetWallpaper
Build Target: Android 2.3
Application name: SetWallpaper
Package name: com.example.SetWallpaper
Create Activity: SetWallpaper

On Clicking Finish SetWallpaper code structure is generated with the necessary Android Packages being imported along with SetWallpaper.java. SetWallpaper class will look like following:

package com.example.SetWallpaper;

import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class SetWallpaper extends Activity {
    Bitmap bitmap;
    int lastImageRef;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonSetWallpaper = (Button)findViewById(R.id.set);
        ImageView imagePreview = (ImageView)findViewById(R.id.preview);
        imagePreview.setImageResource(R.drawable.five);

        buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
 	    @Override
	    public void onClick(View arg0) {
		// TODO Auto-generated method stub
		WallpaperManager myWallpaperManager
		= WallpaperManager.getInstance(getApplicationContext());
		try {
			myWallpaperManager.setResource(R.drawable.five);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    }});
     }
}

Output –The final output:

About the Author

Leave a Reply 17 comments

kevin - September 19, 2011 Reply

Hi nice guide.

I have a question if you can help, i followed the guide and when i run the apk in eclipse all i see it the heloo worls, setwallpaper! no button and no image.

i put my image in each drawable folder and have created a folder called drawable.

Im so new and could really use some help.

thanks

kevin - September 23, 2011 Reply

Hi great article,

I followed this and only get the hello world, setwallpaper! on the screen no button and no wallpaper.

I have added my wallpaper to each directory, now i know i need to add some code to tell the script to use my image. can you share what that code would be?

Thanks

kevin - October 11, 2011 Reply

Would be nice if you actually read some of the comments.
might as well of not had a comment section.

Balu - January 7, 2012 Reply

Can u please give a Full code to run in a eclipse..

nicedice - January 12, 2012 Reply

Hi,
Nice tut thanx – getting some errors in eclipse when amending the SetWallpaper.java:
Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

&
myWallpaperManager.setResource(R.drawable.five);

all throw errors!
any ideas?
thanks
D

nicedice - January 12, 2012 Reply

Hi,
Nice tut thanx – getting some errors in eclipse when amending the SetWallpaper.java:
Button buttonSetWallpaper = (Button)findViewById(R.id.set);
ImageView imagePreview = (ImageView)findViewById(R.id.preview);
imagePreview.setImageResource(R.drawable.five);

&
myWallpaperManager.setResource(R.drawable.five);

all throw errors!
any ideas?

thanks
D

srinivas - April 5, 2012 Reply

dear sir i’ve a doubt the above example gives only one image at a time which we explicitly add to imagePrieview.setImageResource(R.drawable.kiss);

so how can we write code for more than one image display as preview and select one and set as wall paper.

srinivas - April 5, 2012 Reply

dear sir i’ve a doubt the above example gives only one image at a time which we explicitly add to imagePrieview.setImageResource(R.drawable.kiss);

so how can we write code for more than one image display as preview and select one and set as wall paper.

kokesh - April 7, 2012 Reply

id cannot be resolved or is not a field at “Button buttonSetWallpaper = (” line + on few under) – I do have everything exactly as it is here. Any idea?

eldy - September 14, 2012 Reply

my image blur when set as background, can you explain more about the size..

edgarvikernes - December 21, 2012 Reply

Thanks friend it works perfectly :D!!

Suresh - January 30, 2013 Reply

Can you tell how to configure the size of the Wallpaper or configure the scaling of image after setting the wallpaper

sonu - August 6, 2013 Reply

he even did not know how to write for more than one images

sam - August 10, 2013 Reply

while i follow what you are doing here, it leaves out the basic example of using photo taken from with the camera to be set as wallpaper… i can certainly use your example to add resources and set the wallpaper from them, but this gives no idication whatsoever as to how to store an image to the resources from a startActivityForResults method then use the data received to set the wallpaper…

that is it gives no idication as to how to set a wallpaper from a previously unknown data set that the user may define…

Raj - August 20, 2013 Reply

sir , can you give me this code please …??
i want a zip file ..
can you please mail this code to me

caglar - September 11, 2013 Reply

This app makes zoom automatically.
How can I set the image as wallpaper without zoom?

Thank you for your time.

vishal - October 5, 2013 Reply

it was cool..it is perfectly running.but you showed only one image to set as wallpaper..can you tell how to add more images to set as wallpapers

Leave a Reply: