Set ImageView image as Phone Wallpaper in android programmatically

How to set android device background Using WallpaperManager in Android.

In this tutorial we are going to learn about another class of android which is called as android.app.WallpaperManager . This class is used to manage android phone’s background related functionality and if a developer wants to dynamically set phone’s background then this class performs a most important role. So here is the complete step by step tutorial for Set ImageView image as Phone Wallpaper in android programmatically. This type of functionality is mostly used in wallpapers android applications.

android-project-download-code-button

Note: This is the sample wallpaper image, please download and copy this image into drawable folder of your project.

sample_image_wallpaper

How to Set ImageView image as Phone Wallpaper in android programmatically.

Please add SET_WALLPAPER & SET_WALLPAPER_HINTS permission to your AndroidManifest.xml file.

<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>

Code for MainActivity.java file.

package com.android_examples.wallpaper_android_examplescom;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    Button button;
    ImageView imageView;
    WallpaperManager wallpaperManager ;
    Bitmap bitmap1, bitmap2 ;
    DisplayMetrics displayMetrics ;
    int width, height;
    BitmapDrawable bitmapDrawable ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button)findViewById(R.id.button);

        imageView = (ImageView)findViewById(R.id.imageView);

        wallpaperManager  = WallpaperManager.getInstance(getApplicationContext());

        bitmapDrawable = (BitmapDrawable) imageView.getDrawable();

        bitmap1 = bitmapDrawable.getBitmap();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                GetScreenWidthHeight();

                SetBitmapSize();

                wallpaperManager = WallpaperManager.getInstance(MainActivity.this);

                try {

                    wallpaperManager.setBitmap(bitmap2);

                    wallpaperManager.suggestDesiredDimensions(width, height);


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

    public void GetScreenWidthHeight(){

        displayMetrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        width = displayMetrics.widthPixels;

        height = displayMetrics.heightPixels;

    }

    public void SetBitmapSize(){

        bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.wallpaper_android_examplescom.MainActivity">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:src="@drawable/sample_image_wallpaper"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click here to Set this image as Wallpaper in android programmatically"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="94dp" />

</RelativeLayout>

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android_examples.wallpaper_android_examplescom">

    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Screenshot of Application:

Set ImageView image as Phone Wallpaper in android programmatically

 

Screenshot after setting up imageview image as wallpaper :

wallpapermanager-1

Click here to download Set ImageView image as Phone Wallpaper in android programmatically project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *