0

in UWP apps i can use this code to set a StorageFile as a device Wallpaper for both Desktop and Lockscreen:

public static async System.Threading.Tasks.Task ApplyWallpaper(StorageFile storageFile)
{
   await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(storageFile);
   await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(storageFile);
}

looking for Xamarin.Android I found the WallpaperManager but I dont really how to use it exactly.

static WallpaperManager myWallpaperManager;
public static void ApplyToDesktop(Bitmap bitmap)
{
   myWallpaperManager.SetBitmap(bitmap);
}

can you tell me the equivalent of the UWP code for the Xamarin.Android?

Asem Khen
  • 317
  • 3
  • 15

1 Answers1

1

First, add permission in the AndroidManifest.xml:

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

Then, set the wallpaper, sample here is the name of image in the Drawable folder:

public void setWallpaper() {

    Bitmap bitmap = BitmapFactory.DecodeResource(Resources,Resource.Drawable.sample);

    WallpaperManager manager = WallpaperManager.GetInstance(ApplicationContext);

    manager.SetBitmap(bitmap);
    //or
    manager.SetBitmap(bitmap, null, true, WallpaperManagerFlags.Lock);

}

I uploaded a sample project here and you can check it.

nevermore
  • 15,432
  • 1
  • 12
  • 30