Tech Blog

Android

How to programmatically get android launcher wallpaper in your app

By Bansari Pandit2 min read
← Back to blog

There are multiple ways to achieve the goal of displaying the current system wallpaper as the application background. All are mentioned below : 

1. By Using Windowmanager class

It is the easiest way to use system wallpaper in which we can get a drawable object of system wallpaper image using WindowManager class of Android and set it as the activity’s main view background. 

WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
LinearLayout linearMain = findViewById(R.id.linearMain);
linearMain.setBackground(wallpaperDrawable);

2. By Setting Activity theme

Another method is to set Activity’s theme as system wallpaper. To do that, we need to style the theme in the styles.xml file and use it in overriding the getTheme() method of the Activity.

<style name="Theme_Wallpaper">
   <item name="android:windowBackground">@android:color/transparent</item>
   <item name="android:windowShowWallpaper">true</item>
</style>

@Override
public Resources.Theme getTheme() {
        Resources.Theme theme = super.getTheme();
        theme.applyStyle(R.style.Theme_Wallpaper, true);
        return theme;
}

3. When working with WebView

For WebView, we need to use WallpaperManager as used in solution 1 but need to store that image in device storage or asset folder of the app and use that image file as a background in the HTML code. Also, we need to get storage permission to save image. In the below example, we are storing it in the asset folder :

WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
Bitmap img = ((BitmapDrawable)wallpaperDrawable).getBitmap();
//store bitmap in the asset folder
ContextWrapper cw = new ContextWrapper(getBaseContext());
File directory = cw.getDir("assets", Context.MODE_PRIVATE);
File mypath = new File(directory, "system_wallpaper.jpeg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
             img.compress(Bitmap.CompressFormat.PNG, 0, fos);
             fos.close();
   } catch (Exception e) {
             Logger.logError(e);
   }

Keep reading

You may be interested in

Browse all articles →
Android Download file with Data URL
July 23, 20241 min read

Android Download file with Data URL

What is a Data URL? Data URLs are the type of URL scheme that is used to embed usually small types of files. In this URL, inline files are prefixed with the data: MIME type. data:[<mediatype>][;base64],<data> To download files having Data URLs inside Android: Firstly, we need to get an extension of the file from […]

By Bansari PanditRead More →
Download the file with blob URL in Android WebView
July 23, 20242 min read

Download the file with blob URL in Android WebView

What is the Blob URL? Blob URLs can only be generated by the browser with use of internal mapping and used within the current document while it’s open which means it is valid for the same session (ie. the life of the page/document). The blob URL looks like this: blob:http://example.com/550e8400-e29b-41d4-a716-446655440000 Below is the sample HTML […]

By Bansari PanditRead More →
Key Terminologies in Android Enterprise
December 20, 20227 min read

Key Terminologies in Android Enterprise

Android Enterprise is a Google-led initiative to enable the use of Android devices and applications in a workspace. This program offers APIs and tools for developers to integrate support of Android devices into the EMM (enterprise mobility management) or UEM (Unified Endpoint Management) solutions. The following are the key terms associated with Android Enterprise. This […]

By Sandeep KadamRead More →