Android: Set Wallpaper from Resource Drawable


Key points:


1. Save a .png file named: "wallpaper_img.png" in res/drawable folder 

2. a different thread has been used, because setting wallpaper is a time consuming task. we don't want our UI thread non-responsive, right? 😛 

3. handler is used to get back to the original UI thread 

Main Layout File (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="match_parent"
              android:layout_height="match_parent" >

    <Button android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Set Wallpaper"
            android:onClick="setWallpaper" />

</LinearLayout>


AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8"/>
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    <application android:label="@string/app_name">
        <activity android:name="MyActivity"
                  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> 

Main Activity (MyActivity.java)


package com.example;

import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MyActivity extends Activity {

    Handler handler;

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

        handler = new Handler();
    }

    public void setWallpaper (View btnSetWallpaper) {

        Thread thread = new Thread() {

            public void run () {

                WallpaperManager wallpaperManager = WallpaperManager.getInstance(MyActivity.this);

                try {

                    Drawable drawable = getResources().getDrawable(R.drawable.wallpaper_img);
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    wallpaperManager.setBitmap(bitmap);

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MyActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MyActivity.this, "An Error occurred", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

        };

        thread.start();

    }
}

Advertisements

One thought on “Android: Set Wallpaper from Resource Drawable

  1. here wallpaper is not scrollable.it is static for all pannels.if i want make it scrollable than what is solution
    ??

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s