Search code examples
androidandroid-wallpaper

How to set a wallpaper as the homescreen full screen


I have a wallpaper with optimum size to my phone:

myWallpaper

And I want set it as the homescreen by:

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(this);
String imageFilePath = myWallpaperPath;
Bitmap myBitmap = BitmapFactory.decodeFile(imageFilePath);
if (myBitmap != null) {
    try {
        myWallpaperManager.setBitmap(myBitmap);
    } catch (IOException e) {}
} else {}

My problem is that myWallpaper is cropped and then sets as homescreen. I want to set it with full size and quality.


Solution

  • Add this code on your view click

            GetScreenWidthHeight();
    
            SetBitmapSize();
    
            wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
    
            try {
                wallpaperManager.setBitmap(bitmap2);
                wallpaperManager.suggestDesiredDimensions(width, height);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    
    public void GetScreenWidthHeight(){
    
        displayMetrics = new DisplayMetrics();
    
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    
        width = displayMetrics.widthPixels;
    
        height = displayMetrics.heightPixels;
    }
    
    public void SetBitmapSize(){
    
        bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);
    }
    

    Add the below permissions to your project:

    <uses-permission android:name="android.permission.SET_WALLPAPER" />
    <uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
    

    For a full implementation, check out this link here.