Unit with Crooked Nuts
//--------------------
Unit Android.JNI.Toast;
Java Bridge class imported by hand by Brian Long (http://blong.com)
Interface
{$IFDEF ANDROID}
Uses
Androidapi.jnibridge,
Androidapi.JNI.JavaTypes,
Androidapi.JNI.GraphicsContentViewText;
{$ENDIF}
{$IFDEF ANDROID}
Type
Ttoastlength = (longtoast, shorttoast);
Jtoast = interface;
Jtoastclass = Interface (jobjectclass)
[' {69e2d233-b9d3-4f3e-b882-474c8e1d50e9} ']
{property methods}
function _getlength_long:integer; Cdecl
function _getlength_short:integer; cdecl;
{Methods}
function init (context:jcontext): Jtoast; cdecl; overload;
function Maketext (context:jcontext; text:jcharsequence; duration:integer)
: jtoast; cdecl;
{Properties}
Property Length_long:integer Read _getlength_long;
Property Length_short:integer Read _getlength_short;
End;
[Javasignature (' Android/widget/toast ')]
Jtoast = Interface (jobject)
[' {fd81cc32-bfbc-4838-8893-9dd01de47b00} ']
{Methods}
procedure Cancel; Cdecl
function Getduration:integer; Cdecl
function Getgravity:integer; Cdecl
function Gethorizontalmargin:single; Cdecl
function Getverticalmargin:single; Cdecl
function Getview:jview; Cdecl
function Getxoffset:integer; Cdecl
function Getyoffset:integer; Cdecl
Procedure Setduration (Value:integer); Cdecl
Procedure setgravity (Gravity, Xoffset, Yoffset:integer); Cdecl
Procedure SetMargin (HorizontalMargin, Verticalmargin:single); Cdecl
Procedure SetText (s:jcharsequence); Cdecl
Procedure Setview (View:jview); Cdecl
Procedure Show; Cdecl
End
Tjtoast = Class (Tjavagenericimport<jtoastclass, Jtoast>)
End
Procedure Toast (const msg:string; duration:ttoastlength = shorttoast);
{$ENDIF}
Implementation
{$IFDEF ANDROID}
Uses
FMX. Helpers.android,
androidapi.helpers;//Plus, otherwise sharedactivitycontext prompt error,
Procedure Toast (const msg:string; duration:ttoastlength);
Var
Toastlength:integer;
Begin
If Duration = Shorttoast Then
Toastlength: = TJToast.JavaClass.LENGTH_SHORT
Else
Toastlength: = TJToast.JavaClass.LENGTH_LONG;
Callinuithread (
Procedure
Begin
TJToast.JavaClass.makeText (Sharedactivitycontext, Strtojcharsequence (MSG),
Toastlength). Show
End);
End
{$ENDIF}
End.
//-------------------
On the page used:
Uses
Android.JNI.Toast,
Procedure Tform2.formkeyup (Sender:tobject; var Key:word; var Keychar:char;
Shift:tshiftstate);
Begin
if (key = Vkhardwareback) or (key = Vkescape) Then
Begin
If Isfirstback Then
Begin
Isfirstback: = False;
Key: = 0;
timer1.enabled: = True;
Toast (' Press again to exit ', shorttoast);
Toast (' Press again to exit ', shorttoast);
End
Else
Begin
End
End
End
On the form (TFORM2) onkeyup-->formkeyup
Delphi XE inside Sharedactivitycontext hint error