Get access to over 100 FireMonkey cross platform samples for Android, IOS, OSX, Windows, and Linux!

AndroidAppmethodCode SnippetDelphiFiremonkey

Code Snippet To Set The Device Wallpaper In Delphi XE7 Firemonkey On Android

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE7 Firemonkey Android Wallpaper Code Snippet | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSDeveloper Todd Frankson has posted a code snippet for setting the device wallpaper in Android with Delphi XE7 Firemonkey. The original post is on the Embarcadero Forum but I have duplicated it here. Android has a class called the WallpaperManager which allows you to set the system wallpaper and the code uses this class. Additionally the code uses the Android BitmapFactory class. Todd recommends using the Java2op tool to generate the necessary files but you can also access the Object Pascal wrapper code for WallpaperManager on Github as well (and BitmapFactory). The code is pretty straightforward. A TImageViewer is used to save a JPEG out to the filesystem. The JPEG is loaded into the BitmapFactory and then the bitmap is set via the WallpaperManager. This code should also work with Appmethod. Here is the code snippet:
Here's a way to Set the wallpaper.don't know how right or wrong it is).

Live wallpaper is not to much different, so this is a starting point

1. Use Java2op to generate the delphi bridge files of all the wallpaper classes.

2 New Fmx poject
3. Add the units to your uses clause along with:
4. place the following on the form:
Button1: TButton;
Image1: TImageViewer;

5. Load an image into Image1 in design time. and set Button1 onclick to below.

procedure TForm1.Button1Click(Sender: TObject);
Var
FWallpaperManager:jwallpapermanager;

Factoryoptions: JBitmapFactory_Options;
aScreenSize: TPoint;
Widthofscreen, Heightofscreen: Integer;
FFiletoopen:String;
begin
{Create a filename to save the image to}
FFiletoopen:=System.IOUtils.TPath.Combine(System.IOUtils.tpath.getdocumentspath,'Thefile.jpg');
{Save the image}
Image1.Bitmap.savetofile(FFiletoopen);
{Create  JBitmap options }
Factoryoptions:=TJBitmapFactory_Options.Create;
{Read up on these in the android API}
Factoryoptions.inJustDecodeBounds := true;
Factoryoptions.inPreferQualityOverSpeed:=True;
Factoryoptions.inJustDecodeBounds := false;
{Get the wallpaper manager instance}
FWallpaperManager:=tjwallpapermanager.Wrap((SharedActivityContext.getSystemService
(TJContext.JavaClass.WALLPAPER_SERVICE) as  ILocalObject).GetObjectID);
{Load the image we saved}
TheBitmaptoShow:=Tjbitmapfactory.JavaClass.decodeFile(StringToJString(FFiletoopen),Factoryoptions);
{Only change the wallpaper if the Bitmap loads}
if TheBitmaptoShow <>NIL then
Begin
{Set the Wallpaper}
FWallpaperManager.setBitmap(TheBitmaptoShow);
End;
end;

Head over and see the post on the Embarcadero forum for Delphi XE7 Firemonkey on Android.

Have Delphi Firemonkey questions? Ask and get answers on StackOverflow.

Related posts
DelphiDemoFiremonkeyLinuxOSXShowcaseWindows

AutoBlogAI: FireMonkey Client To Leverage LLMs And Generative AI For Blogging

DelphiFiremonkeyShowcaseUtilityWindows

Unleashing Creativity With Song Writer AI: A Deep Dive

DelphiFiremonkeyShowcaseWindows

How To Build Stable Diffusion Text To Image Prompts

AndroidC++BuilderDelphiFiremonkeyIOSOSXWindows

FireMonkey 10.4.2 Features Updated iOS 14, Android 11, And macOS 11 Support Plus Hundreds Of Fixes

Sign up for our Newsletter and
stay informed

Leave a Reply