Skip to content
Related Articles

Related Articles

How to Set an Image as Wallpaper Programmatically in Android?
  • Last Updated : 23 Feb, 2021

Setting wallpaper in Android programmatically is helpful when the application is fetching wallpapers from the API library and asking the user whether to set the wallpaper for the home screen or not. In this article, it’s been discussed how to set a sample image as the home screen wallpaper programmatically. Have a look at the following image to get an idea of how that is going to be work after implementation. Note that we are going to implement this project using the Java language. 

 Set an Image as Wallpaper Programmatically in Android

Steps to implement the setting up the wallpaper programmatically

Step 1: Create a New Project

Step 2: Now add the permission to the AndroidManifest.xml file

XML



filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
    package="com.adityamshidlyali.setimageaswallpaper">
  
    <!--access permission to set the wallpaper-->
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

chevron_right


  • Refer to the following image if unable to locate the AndroidManifest.xml file to invoke the permission.

Set an Image as Wallpaper Programmatically in Android

Step 3: Now import some images to the drawable folder

  • Import some images to the drawable folder or can fetch the images from the API libraries.
  • In this case, a sample GeeksforGeeks logo image has been imported to the drawable folder.
  • The drawable folder can be got under the app > src > main > res > drawable
  • If unable to locate the drawable folder refer to the following image.

Set an Image as Wallpaper Programmatically in Android

Step 4: Working with the activity_main.xml file

  • Invoke the simple layout given below in the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:ignore="HardcodedText">
  
    <!--layout to bound the width and height of the wallpaper preview-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="400dp">
        <!--a sample image view for the preview purpose-->
        <ImageView
            android:id="@+id/wallpaper_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:scaleType="centerCrop"
            android:src="@drawable/wallpaper" />
    </LinearLayout>
  
    <!--button which sets the image as wallpaper-->
    <Button
        android:id="@+id/set_wallpaper_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Set As Wallpaper" />
  
</LinearLayout>

chevron_right


Following Output UI is Produced: 

Set an Image as Wallpaper Programmatically in Android

Step 5: Working with the MainActivity.java file 

  • Handle the button to set the desired wallpaper using WallpaperManager.
  • Invoke the following code in the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.WallpaperManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
  
public class MainActivity extends AppCompatActivity {
  
    // button to set the home screen wallpaper when clicked
    Button bSetWallpaper;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // creating the instance of the WallpaperManager
        final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
  
        // handle the set wallpaper button to set the desired wallpaper for home screen
        bSetWallpaper = findViewById(R.id.set_wallpaper_button);
        bSetWallpaper.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceType")
            @Override
            public void onClick(View v) {
                try {
                    // set the wallpaper by calling the setResource function and 
                      // passing the drawable file
                    wallpaperManager.setResource(R.drawable.wallpaper);
                } catch (IOException e) {
                    // here the errors can be logged instead of printStackTrace
                    e.printStackTrace();
                }
            }
        });
    }
}

chevron_right


Output: Run on Emulator


Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :