How to Set WallPaper in AndroidHow to Set WallPaper in Android

Want to set my own image as wallpaper. how can achieve it.

Answer 1

To Set the Wallpaper we must declare following permission in manifest file.
   
 
 

If you use an Image to set as wallpaper, The image may not fit to the Screen, it may be smaller or larger than the device screen, To overcome this problem or if we want that Image should to the screen for all the devices with different screen sizes, we need to use DisplayMatrics to current device width and height.
   
// get the Image to as Bitmap 
Bitmap bitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.id.image));

    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    // get the height and width of screen 
    int height = metrics.heightPixels; 
    int width = metrics.widthPixels;
           
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
      try {
              wallpaperManager.setBitmap(bitmap);
                
              wallpaperManager.suggestDesiredDimensions(width, height);
             Toast.makeText(this, "Wallpaper Set", Toast.LENGTH_SHORT).show();

          } catch (IOException e) {
                  e.printStackTrace();
           }

SUBMIT YOUR ANSWER

  |  
 
 
 

Preview :