Setting an image as a wallpaper

3


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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.SetWallpaper"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
    <uses-permission android:name="android.permission.SET_WALLPAPER">
    </uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SetWallpaper" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

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:

  • srinivas

    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.

  • http://kokeshnet.com kokesh

    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

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

Proudly designed by Theme Junkie.