Monday, March 28, 2011

Set Wallpaper using WallpaperManager

In this exercise, a picture (packed inside our app) will be set as system wallpaper, using WallpaperManager.

* Please note that in order to use WallpaperManager, minimum API level have to be set to 5.

Set Wallpaper using WallpaperManager
Set Wallpaper using WallpaperManager

First of all, prepare a picture (wallpaper.png in my case) and save it in /res/drawable/ folder of your project.

main code
package com.exercise.AndroidWallpaper;

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 AndroidWallpaper extends Activity {
 
 Bitmap bitmap;
 
   /** 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.wallpaper);
      
       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.wallpaper);
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
    
   }});

   }
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<Button
  android:id="@+id/set"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="Set Wallpaper"
  />
<ImageView
  android:id="@+id/preview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  />
</LinearLayout>


Finally, you have to modify AndroidManifest.xml to grant permission of "android.permission.SET_WALLPAPER".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.exercise.AndroidWallpaper"
     android:versionCode="1"
     android:versionName="1.0">
   <uses-sdk android:minSdkVersion="5" />
   <uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>

   <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".AndroidWallpaper"
                 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>


Download the files.

Updated@2016-03-03:
Load photo and set Wallpaper


17 comments:

  1. Hi, thanks for the post.
    I need a little help.
    The following code shows wallpapers to set as the wallpaper.

    startActivityForResult(Intent.createChooser(new Intent(Intent.ACTION_SET_WALLPAPER), "Wallpaper Sets"), 0);

    It works well. But i don't want to set the wallpaper that selected at that moment. I just need to know which one is selected. I mean i would like to know selected wallpaper and use it the another application. How can i do that?

    Regards...

    ReplyDelete
  2. hello Erol,

    In my understanding: you already started another activity, so the control have been passed to the activity, you cannot force it not to set wallpaper.

    If you want to set background of your activity using system wallpaper, refer here: Use system wallpaper as application's wallpaper

    ReplyDelete
  3. Hi again, could you add a wallpaper manager tutorial on your blog? But this time, you can use the current wallpapers on the emulator?
    I'am so beginner for android.

    Thanks..

    ReplyDelete
  4. hello Erol,

    Thx for your comment.
    The picture on the post was captured in Emulator, with my own wallpaper.

    I'm a beginner too:)

    ReplyDelete
  5. Hi,
    Where are the default wallpapers located?
    I searched them but I couldn't find.
    I need the reach them.

    Thanks..

    ReplyDelete
  6. hello Erol,

    It should be in /data/data/com.android.settings/files/wallpaper

    But I think you cannot access it directly, except that your phone have been rooted.

    ReplyDelete
  7. In my application i cannot access the default wallpapers?
    I think i should create a wallpaper folder in my sdcard and put there some wallpapers so i use them for my applicaton. or you have a better idea ?

    Thanks you help me a lot.
    Your blog is so useful.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hello,
      How set wallpaper from my site?

      Thanks.

      Delete
  9. Yes sorry,
    i create site with jquerymobile/PHP and wabview with eclipse a like make a button in gallery for set wallpaper on android mobile.

    Thanks

    ReplyDelete
  10. hello w,

    Sorry! I have no idea about it.

    ReplyDelete
  11. Hey um if you want to declare multiple images, and also be able to set anyone of them, what do u do? I have been able to do this, but after the 4.4 release, eclipse doesn't seem to work properly

    ReplyDelete
  12. Hey i am getting an error on
    myWallpaperManager.setResource(R.drawable.wallpaper)
    supplying wrong type of resource identifier.. what should i do?

    ReplyDelete
  13. hello Azka,

    The resource must be a valid PNG or JPEG image.

    Can you manually set your image as wallpaper?

    ReplyDelete
  14. here wallpaper if not scrollable.if i want tomake it srollable than?

    ReplyDelete
  15. myWallpaperManager.setResource(R.drawable.wallpaper);
    in this line, an error occurs " Expected resource of type raw..."
    what to do...?

    ReplyDelete