someone found how to do an android  "toast" message with delphi xe5?

Comments

  1. Nope.  Not as a readily wrapped object way, anyways...

    ReplyDelete
  2. i'll not sleep until i get it (or someone else) :D

    edit:
    i give up [ for a while :) ], i have to work tomorrow... :|

    ReplyDelete
  3. I thought it would be working like that ...
    ===
    unit psJToast;

    interface

    uses
      Androidapi.JNIBridge,
      Androidapi.JNI.JavaTypes,
      Androidapi.JNI.Os,
      Androidapi.JNI.Net,
      Androidapi.JNI.Util,
      Androidapi.JNI.GraphicsContentViewText;

    type
      JToast = interface;

      JToastClass = interface(JViewClass)
        ['{0829BB43-F89D-473F-9A59-FB44CA97ED08}']
        { Static methods of toast-class go here }
        function makeText(context: JContext; resId, duration: Integer): JToast;
          overload; cdecl;
        function makeText(context: JContext; text: JCharSequence; duration: Integer)
          : JToast; overload; cdecl;
        { Property Methods }
        function _GetLENGTH_LONG: Integer;
        function _GetLENGTH_SHORT: Integer;
        { Properties }
        property LENGTH_LONG: Integer read _GetLENGTH_LONG;
        property LENGTH_SHORT: Integer read _GetLENGTH_SHORT;
      end;

      [JavaSignature('android/widget/Toast')]
      JToast = interface(JView)
        ['{43688786-E77D-40D1-A5DD-7C7AF1BDA20C}']
        { Instance methods of toast-class go here }
        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(duration: Integer); cdecl;
        procedure setGravity(gravity, xOffset, yOffset: Integer); cdecl;
        procedure setMargin(horizontalMargin, verticalMargin: single); cdecl;
        procedure setText(resId: Integer); cdecl; overload;
        procedure setText(s: JCharSequence); cdecl; overload;
        procedure setView(view: JView); cdecl;
        procedure show; cdecl;
      end;

      TJToast = class(TJavaGenericImport)
      end;

    implementation

    end.
    ===

    === 
    unit JToastTestbedMainForm;

    interface

    uses
      System.SysUtils, System.Types, System.UITypes, System.Classes,
      System.Variants,
      FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private-Deklarationen }
      public
        { Public-Deklarationen }
      end;

    var
      Form1: TForm1;

    implementation

    uses psJToast, Androidapi.JNI.Javatypes,
      FMX.Platform.Android, Androidapi.JNI.OS;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      TJLooper.JavaClass.prepare;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    var
      t: JToast;
      cs: JString;
      chs: JCharSequence;
    begin
      cs := StringToJString
        ('Good job - but I want my external keyboard functioning properly :-)');
      chs := cs.subSequence(0, 10);
      TJToast.JavaClass.makeText(MainActivity.getApplication, chs,
        TJToast.JavaClass.LENGTH_LONG).Show;
    end;

    {$R *.fmx}

    end.
    ===

    But it is not :-(

    I think I do not get the correct context, but I have no idea what else to try.

    I know it does not really make sense to do this single plattform besides of educational reasons, but I would really like to know how to do it with JNI - does anybody have an idea?

    ReplyDelete
  4. Perhaps the eminent Brian Long can shed some light on the mystery?

    ReplyDelete
  5. This is my unit. It will be one of the things I demo in my CodeRage 8 talk on Android API stuff. Other things under consideration are launching built-in activities like SMS, email, maps etc., calling .jar-based code, a splash screen that checks FMX requirements, etc.

    unit Androidapi.JNI.Toast;

    //Java bridge class imported by hand by Brian Long (http://blong.com)

    interface

    uses
      Androidapi.JNIBridge,
      Androidapi.JNI.JavaTypes,
      Androidapi.JNI.GraphicsContentViewText;

    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) end;

    procedure Toast(const Msg: string; Duration: TToastLength = ShortToast);

    implementation

    uses
      FMX.Helpers.Android;

    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;

    end.

    ReplyDelete
  6. Cool. Thanks a lot for the source - very insightful.

    [Roland spends Brian a virtual beer.]

    ReplyDelete
  7. Brian Long When you implemented the interface are the GUID's pulled from somewhere or just randomly made up to be a unique ID?

    ReplyDelete
  8. CTL shift g when doing what? Typing out the interface?

    ReplyDelete
  9. When you want to insert a snipet like ['{DCFB0323-A1D8-4BD5-9268-F47CFF19CEC5}'] in the IDE.

    ReplyDelete
  10. Press CTRL+SHIFT+G and you get something like ['{DCFB0323-A1D8-4BD6-9268-F47CFF19CAA5}'].

    ReplyDelete
  11. I see, I was thinking the GUID's referenced something somewhere else but it's just a unique identifier for the interface or class. Thx.

    ReplyDelete
  12. They are not pulled from somewhere beyond CTRL-SHIFT-G any GUID should do.

    ReplyDelete
  13. BTW - I could not resist and developed a FMX-Toast (or shall I write FM-Toast) component.
    It should will work x-plattform on WIN32/WIN64/MACOSX/Android/iOS ...

    https://docs.google.com/file/d/0B_mUUtJe8v84NEZjTjZCemFDd1U/edit?usp=sharing

    Just compile and install.

    Your welcome.

    ReplyDelete
  14. Thank you! Worked very well. Even to me, as a completly noob with Java stuff and Android I was able to make my first Toast :)

    ReplyDelete
  15. Good to read. You can find updates of the FMX wrapper under https://www.cybertribe.de/info//components/fmx/toast in the future.

    ReplyDelete
  16. sorry to dig up an old thread...I have this working great on android...but how do you do a similar toast message on iOS? .Thanks!

    ReplyDelete
  17. Hi Brian,
    you can either use my component - it is mimicing a toast based on top of FMX or you do it IOS natively by using UIAlertView see more thorrough explanations here http://stackoverflow.com/questions/3737911/how-to-display-temporary-popup-message-on-iphone-ipad-ios .

    ReplyDelete
  18. hi,ok, I will look into using your fmx toast for iOS...(I was using a java intent for android)

    ReplyDelete
  19. Ok. Just let me know if it is working for you or if you need support to get it running.

    ReplyDelete
  20. Hi..do I just need to use
    toast1.now('text to show');
    ?
    as it does not seem to work on the iOS simulator?

    ReplyDelete
  21. Hi. Should be working like that. Maybe the duration is set to 0 or perhabs it is a problem with the component. You might need to make it a child of a panel that is in the foreground.

    ReplyDelete
  22. HI..for testing I just dropped a button on a blank form and added the fmx toast component and set code as above...setting a duration to 100 does not change the beaviour on a ioS simulator....after clicking on button1....nothing happens (and the button is not released)

    ReplyDelete
  23. Hi Brian,
    I checked it - you need to set the stroke thickness to a value > 0. Then it is working.
    I will change the create of the component so it comes up with reasonable values right away. Sorry for the hassle.
    Here is a configuration with which it works ...
      object Toast1: TToast
        ToastBoxStrokeColor = claNull
        TextColor = claSlateblue
        ToastBoxColor = claSlategray
        ToastBoxStrokeDash = sdSolid
        TextAlign = taCenter
        Duration = 1000
        OptimalWidth = False
        ToastBoxAlign = alCenter
        ToastBoxHeight = 50.000000000000000000
        ToastBoxStrokeThickness = 1.000000000000000000
        ToastBoxWidth = 100.000000000000000000
        Left = 64
        Top = 80
      end

    ReplyDelete
  24. BTW the duration is in milliseconds so 100 might be a little short.

    ReplyDelete
  25. hummm.setting the strokethickness to 1 made the button return...but i still dont see the message (with duration set to 1000), on iOS simulator (i.e fmx mobile)...are you able to duplicate the same problem on ios simulator, or?
    thanks for the help

    ReplyDelete
  26. hi, Ok, I have your new update..with the new icon for the component of a toaster :)
    still not working for just dropping on a iOS mobile form and via a button1click,in the iossimulator on my mac,
    toast1.Now('testing');

    the button does return after 1 second...but no message shows and the button cant be clicked again afterwards...
    hope this helps...I get the impression I am the first person to test for iOS mobile?

    ReplyDelete
  27. ps,no errors though when running in debugger..I will try running on an actual device...

    ReplyDelete
  28. Hi Brian,
    with XE5 it is working without a problem here in combination with the iOS
    simulator and a physical iOS device in debug and release mode.
    Could it be that your toast is positioned in a not visible area of the
    simulator?
    Try to use the ToastBoxAlign property and set it to alCenter.
    You have two possibilities to specify the location of the toastbox either
    by using the toastboxposition or by using the toastboxalign property. Find
    attached my testproject,


    Am 15. Februar 2014 13:40 schrieb Roland Kossow <****@**>:

    > Hi.
    > Which Delphi Version Do you use?

    ReplyDelete
  29. Hi Brian.
    I also tested with Android now.
    Everything (WIN32,WIN64,MACOSX,Android device, iOS Simpulator and iOS
    Device) seems to be working with the - once more - updated component under
    ...
    https://www.cybertribe.de/info/components/fmx/toast/FMXToast.zip

    Please let me know if it does not work for you and perhabs send me your
    testproject so I can have a look into it.

    Best regards

    Roland


    Am 15. Februar 2014 15:51 schrieb Roland Kossow <****@**>:

    > Hi Brian,
    > with XE5 it is working without a problem here in combination with the iOS
    > simulator and a physical iOS device in debug and release mode.
    > Could it be that your toast is positioned in a not visible area of the
    > simulator?
    > Try to use the ToastBoxAlign property and set it to alCenter.
    > You have two possibilities to specify the location of the toastbox either
    > by using the toastboxposition or by using the toastboxalign property. Find
    > attached my testproject,
    >
    >
    > Am 15. Februar 2014 13:40 schrieb Roland Kossow <****@**>:
    >
    > Hi.
    >> Which Delphi Version Do you use?

    ReplyDelete
  30. hi...ok, the align was set to alnone...I have set that to alcenter...and set the position to 10 for x and 10 for y
    the button returns from a click straight away now and you can click as many times as you like now...with your latest update...but I am still not seeing the message box in the simulator..this is with XE5 update 2

    you can download my test project (very simple)
    http://www.weather-display.com/downloadfiles/projecttoast.zip

    you say you have a test project attached?

    thanks for the help!

    ReplyDelete
  31. Hi Brian,
    everything is fine, you just have to set the color properties to values
    different than Null.
    Then it is working.
    They should be initialized with values different than Null in the current
    version of the component.


    Best regards

    Roland

    ReplyDelete
  32. I tested it now with your testproject - which is about the same as my
    testproject.


    2014-02-15 19:03 GMT+01:00 Roland Kossow <****@**>:

    > Hi Brian,
    > everything is fine, you just have to set the color properties to values
    > different than Null.
    > Then it is working.
    > They should be initialized with values different than Null in the current
    > version of the component.
    >
    >
    > Best regards
    >
    > Roland
    >
    >

    ReplyDelete
  33. ah ha...now its working....thanks :)
    hopefully I have been a good green guinea pig
    (I had assumed the null colour would have at least defaulted to black)
    one question: is there a way to make the box transparent , i.e to take on the colour of the background?

    ReplyDelete
  34. Hi Brian,
    you were an excellent betatester and the component improved already a lot
    because of our conversation - it now defaults to non-null color values and
    supports transparency (see below) :-) Thanks for testing.
    Considering transparency: I added an Opacity property to the newly updated
    0.3 Alpha which can be downloaded now under ...
    https://www.cybertribe.de/info/components/fmx/toast/

    Happy testing!

    Best regards

    Roland

    ReplyDelete
  35. excellent,...the transparency works via the opacity, and thinking about it, I can set the background colour to what the background colour is off the app anyway too....one problem though with the opacity setting is that makes the text also opaque....ie harder to read..(e.g using 0.5).but i suppose there is no way to set the rectangle opacity but not the text opacity (which is the text in the rectangle)?

    ReplyDelete
  36. Hi Brian,
    yes - I will try to fix that (text opacity) in the next version. Should be
    possible ...
    But not right now - tomorrow.

    Regards

    Roland

    ReplyDelete
  37. BTW - if you just want the box to disappear set the textboxcolor value to
    NULL.


    2014-02-15 19:42 GMT+01:00 Roland Kossow <****@**>:

    > Hi Brian,
    > yes - I will try to fix that (text opacity) in the next version. Should be
    > possible ...
    > But not right now - tomorrow.
    >
    > Regards
    >
    > Roland
    >
    >

    ReplyDelete
  38. that works, setting textbox color to null
    if you can get the text to not be transparent, but the box to be transparent, then I reckong that would look great

    ReplyDelete
  39. ps, I am going to use this in my OSX program too....good way to show events/info in the program :)

    ReplyDelete
  40. one more setting you might want to make public is the
     self.FRectangle.AnimateFloat('Opacity', 0, 0.3);

    setting that to 0.8 makes the message linger longer on the fade...
    :)

    ReplyDelete
  41. Hi.
    New version uploaded ...

    https://www.cybertribe.de/info/components/fmx/toast/FMXToast.zip

    - AppearanceDuration (in milliseconds)
    - DisappearanceDuration (in millseconds)
    - Text has always an opacity of 1

    Major changes could lead to new bugs - happy testing!

    Best regards

    Roland

    ReplyDelete
  42. Great - thanks for the feedback.
    Further feature requests are welcome.

    Regards

    Roland

    ReplyDelete

Post a Comment