Latmos
Latmos

Reputation: 65

Setting Home Screen wallpaper programmatically (Changing home screen and lock screen)

I have an application that change home screen wallpaper if you click "Set as Wallpaper button" but if you click this button changing home screen and lock screen walpaper. I want to change just home screen wallpaper

Here is the code

 button1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    WallpaperManager myWallpaperManager
                            = WallpaperManager.getInstance(getApplicationContext());

                    try {


             myWallpaperManager.setResource(+ R.drawable.image_0);

                        Toast.makeText(
                                getBaseContext(),
                                "Wallpaper has been updated",
                                Toast.LENGTH_SHORT).show();

                    }
                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });

Upvotes: 2

Views: 5389

Answers (2)

Muaz Khan
Muaz Khan

Reputation: 27

you can do that by using FLAG_SYSTEM

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {   
    wallpaperManager.setStream(input, null, false, WallpaperManager.FLAG_SYSTEM) 
}

Upvotes: 0

Milind Gandhi
Milind Gandhi

Reputation: 91

You can set the Image as Wallpaper using WallpaperManager.

To set the image as wallpaper using...

myWallpaperManager.setStream(fileInputStream, null, true, WallpaperManager.FLAG_SYSTEM);

To set the image as Lock screen use...

myWallpaperManager.setStream(fileInputStream, null, false, WallpaperManager.FLAG_LOCK);

In this fileInputStream is the inputstream of an image file that you want to set as wallpaper.

You can also set Bitmap as wallpaper and lock screen using...

For Wallpaper:

myWallpaperManager.setBitmap(imageBitmap,null,true,WallpaperManager.FLAG_SYSTEM);

For lock screen:

myWallpaperManager.setBitmap(imagebitmap, null, false, WallpaperManager.FLAG_LOCK);

Upvotes: 7

Related Questions