How to Set an Image as Wallpaper Programmatically in Android?
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.
Steps to implement the setting up the wallpaper programmatically
Step 1: Create a New Project
- To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
- Note that select Java as the programming language.
Step 2: Now add the permission to the AndroidManifest.xml file
- Invoke the following code in the AndroidMainfest.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" 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 > |
Refer to the following image if unable to locate the AndroidManifest.xml file to invoke the permission.
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 w3wiki 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.
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
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout 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" 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 > |
Following Output UI is Produced:
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
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(); } } }); } } |
Kotlin
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; class MainActivity : AppCompatActivity() { // button to set the home screen wallpaper when clicked var bSetWallpaper: Button? = null override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) // creating the instance of the WallpaperManager val wallpaperManager = WallpaperManager.getInstance(applicationContext) // handle the set wallpaper button to set the desired wallpaper for home screen bSetWallpaper = findViewById(R.id.set_wallpaper_button) bSetWallpaper.setOnClickListener(object : OnClickListener() { @SuppressLint ( "ResourceType" ) fun onClick(v: View?) { try { // set the wallpaper by calling the setResource function and // passing the drawable file wallpaperManager.setResource(R.drawable.wallpaper) } catch (e: IOException) { // here the errors can be logged instead of printStackTrace e.printStackTrace() } } }) } } // This code is written by Ujjwal Kumar bhardwaj |