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
Step 2: Now add the permission to the AndroidManifest.xml file
XML
<? xml version = "1.0" encoding = "utf-8" ?>
package = "com.adityamshidlyali.setimageaswallpaper" >
< 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 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.
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
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:orientation = "vertical"
tools:context = ".MainActivity"
tools:ignore = "HardcodedText" >
< LinearLayout
android:layout_width = "match_parent"
android:layout_height = "400dp" >
< 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
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 bSetWallpaper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
bSetWallpaper = findViewById(R.id.set_wallpaper_button);
bSetWallpaper.setOnClickListener( new View.OnClickListener() {
@SuppressLint ( "ResourceType" )
@Override
public void onClick(View v) {
try {
wallpaperManager.setResource(R.drawable.wallpaper);
} catch (IOException e) {
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() {
var bSetWallpaper: Button? = null
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val wallpaperManager = WallpaperManager.getInstance(applicationContext)
bSetWallpaper = findViewById(R.id.set_wallpaper_button)
bSetWallpaper.setOnClickListener(object : OnClickListener() {
@SuppressLint ( "ResourceType" )
fun onClick(v: View?) {
try {
wallpaperManager.setResource(R.drawable.wallpaper)
} catch (e: IOException) {
e.printStackTrace()
}
}
})
}
}
|
Output: Run on Emulator
Last Updated :
27 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...