Android Question How to Set Wallpapers for Home Screen, Lock Screen, or Both Separately?

Javaneh

Member
Hi everyone,
I have some questions and issues related to setting images as wallpapers :
  1. How can I set an image specifically for the home screen, the lock screen, or both? I couldn’t find separate code examples for each of these.
  2. How can I detect if setting the wallpaper was successful? Something similar to the "Wait For" command. For example, I’d like to show a toast message upon success or failure.
  3. I used the following code shared by @Erel on the forum.

    B4X:
        Dim wallpaper As JavaObject
        Dim context As JavaObject
        context.InitializeContext
        wallpaper = wallpaper.InitializeStatic("android.app.WallpaperManager").RunMethod("getInstance", Array(context))
        wallpaper.RunMethod("setBitmap", Array(bim))

    This code simultaneously sets the wallpaper for both the home screen and the lock screen. However, the problem is that when I use this code to set the wallpaper, it takes a few seconds, and then the activity (or the entire app) reloads or refreshes. (In my future projects, I plan to use B4XPages 😁 .) I'm not sure why this happens. Additionally,
    I’m using RecyclerView and the SMM library for this task.

Thank you!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to select the target(s):
B4X:
Private Sub SetWallpaper(bmp As B4XBitmap, LockScreen As Boolean, HomeScreen As Boolean)
    Dim wallpaper As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    wallpaper = wallpaper.InitializeStatic("android.app.WallpaperManager").RunMethod("getInstance", Array(context))
    Dim flags As Int = 0
    If LockScreen Then flags = Bit.Or(flags, 2)
    If HomeScreen Then flags = Bit.Or(flags, 1)
    wallpaper.RunMethod("setBitmap", Array(bmp, Null, True, flags))
End Sub

The intent that was broadcasted when the wallpaper changes, in older versions of Android, is no longer available. I guess that if no exception is raised then you can assume that it was successful.

And you cannot prevent the activity from being recreated. This is a special case that is also true with B4XPages.
 
Upvote 0
Top