Setting Android HomeScreen wallpaper only
How can I set the wallpapers of android programmatically for the options below:
- Set as HomeScreen Only
- Set as Lockscreen only
- Set as Homescreen and Lockscreen
Below is what I have So far but I can't figure out how I can set it for homescreen only.
private void setWallpaper(final int flagSystem) {
Glide.with(this)
.asBitmap()
.load(wallPaper)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getContext());
int wallpaperHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
int wallpaperWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
Point start = new Point(0, 0);
Point end = new Point(bitmap.getWidth(), bitmap.getHeight());
if (bitmap.getWidth() > wallpaperWidth) {
start.x = (bitmap.getWidth() - wallpaperWidth) / 2;
end.x = start.x + wallpaperWidth;
}
if (bitmap.getHeight() > wallpaperHeight) {
start.y = (bitmap.getHeight() - wallpaperHeight) / 2;
end.y = start.y + wallpaperHeight;
}
if (flagSystem == 0) {
wallpaperManager.setBitmap(bitmap, null, true);
} else if (flagSystem == 1) {
wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
} else {
wallpaperManager.setBitmap(bitmap, new Rect(start.x, start.y, end.x, end.y), false);
}
Toast.makeText(getContext(), "Wallpaper set successfully", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
Solution 1:
use WallpaperManager.FLAG_SYSTEM
flag
manager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_SYSTEM);