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.
- 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.
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.
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.