B4J Library [B4X] [XUI] ToastMessage class

Note: uploaded a newer class version (0.30) and its corresponding B4A/B4J demos; this one is much better (read post #6 for details)!

Hi all,

some time ago I wrote a class to port a TostMessageShow-alike method to B4J.
Yesterday I decided it was time to convert it to B4x using XUI. While at it, I added methods here and there and some code to be able to use it with B4A and (hopefully) with B4i.

Please find attached the class (clXToastMessage.bas) and working example code for B4J (tmtestJ.zip) and B4A (tmtest.zip). Note that both the zip files contain a copy of the class so you don't need to download it separately.

I couldn't test it with B4i and, to say it all, I'm not even sure it will work with it "as is" because my coding followed snippets found on the Forum not any direct knowledge on my part. So please, amend it for B4i and post here the revised version.

B4J: class uses JavaObject, JXUI and JFX
B4A: class uses XUI and StringUtils
B4i: I don't know; let me know.

Well, it was a good exercise as a first approach to XUI. Maybe it will be of inspiration for further uses or it will be simply useful to collect hints about wrong code or component misuse..
Let me know, I'm here to learn!


Enjoy
 

Attachments

  • tmtest.zip
    13.3 KB · Views: 602
  • tmtestJ.zip
    6.5 KB · Views: 658
  • clXToastMessage.bas
    10 KB · Views: 748
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not really needed:
B4X:
Public Sub Initialize(Base As Object)
#if B4A
   If (Base Is Activity) Or (Base Is Panel) Then
       Available = True
       mBase = Base 'f1
   End If
#end if
#if B4J
   If Base Is Form Then
       Available = True
       Dim f1 As Form = Base
       mBase = f1.RootPane
   End If
   If Base Is Pane Then
       Available = True
       mBase = Base
   End If
#end if
#if B4i
   If Base Is Page Then
       Available = True
       Dim f1 As Page = Base
       mBase = f1.RootPanel
   End If
   If Base Is Panel Then
       Available = True
       mBase = Base
   End If
#end if

The standard way to do it is to expect a B4XView and let the developer pass an Activity, MainForm.RootPane or Page1.RootPanel (or any other panel).
B4X:
Public Sub Initialize(Base As B4XView)
 mBase = Base

To make it work in B4i you need to add to Initialize:
B4X:
mBase.Tag = Me

And to remove this reference when you remove the view:
B4X:
Private Sub timer1_tick
   timer1.Enabled = False
   mPanel.RemoveViewFromParent
   mBase.Tag = Null
End Sub

Otherwise it will not work with local toasts as the class instance will be released before the timer event.

Note that the timer is not needed here.

B4X:
Dim Duration As Int
If LongDuration Then Duration = LDuration Else Duration = SDuration
Sleep(Duration)
mPanel.SetVisibleAnimated(500, False)
Sleep(500)
mPanel.RemoveViewFromParent
mBase.Tag = Null
This code also fades out the message.

You can also add a fade in effect in the Initialize method:
B4X:
mPanel.Visible = False
mPanel.SetVisibleAnimated(500, True)
 

udg

Expert
Licensed User
Longtime User
The standard way to do it is to expect a B4XView and let the developer pass an Activity, MainForm.RootPane or Page1.RootPanel (or any other panel).
I was worried by the fact that, being the parameter an Object, anything could be thrown at it, but using a B4xView helps it since it will be the compiler to check.
Fade in/out..nice idea. Would you suggest it as a standard behavior or just as an option?
 

udg

Expert
Licensed User
Longtime User
Absolutely. It's a much appreciated suggestion and I'll include it in my next revision for sure. I asked because I don't know if I should set the fading as the only way to show a message or just as an option.
I hope to have some time for programming at lunch time (meanwhile it's better that I show a notice on the first post..)
 

udg

Expert
Licensed User
Longtime User
Hi all,

please find attached to post #1 a new version (rel 0.30) of the class clXToastMessage.
It's based on Erel's hints from post #2

New features:
- optional fade in / fade out effect
- queueing of Toast Messages
- method TMClearAndShow to abruptly stop displaying message and then show a new one

@Erel: please check the use of mBase.Tag since I feel it's not correct in this context (reusing same instance for several messages). TIA

udg
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Change:
B4X:
For i = 0 To m1.Size - 1
       If Not(tempSettings.ContainsKey(m1.GetKeyAt(i))) Then _
           tempSettings.Put(m1.GetKeyAt(i), m1.GetValueAt(i))
   Next
To:
B4X:
For Each key As Object In m1.Keys
       If tempSettings.ContainsKey(key) = False Then
           tempSettings.Put(key, m1.Get(key))
       End If
   Next
There is no GetKeyAt / GetValueAt in B4i and For Each is preferred over those methods.

B4X:
Private Sub DuplicateMap(Source As Map) As Map
   Dim m1 As Map
   m1.Initialize
   For Each key As Object In Source.Keys
       m1.Put(key, Source.Get(key))
   Next
   Return m1
End Sub


Remove this line:
B4X:
    mLbl.Font = DefaultFont

There is no such variable and it is not needed.

The usage of the mbase.Tag looks correct.
 
  • Like
Reactions: udg

LucaMs

Expert
Licensed User
Longtime User
@Erel: please check the use of mBase.Tag since I feel it's not correct in this context (reusing same instance for several messages). TIA
Hi.
I'm an Erel's cousin so... [kidding :D]

I can not see where your are really using the tag of your class/instance. You set it in three places of your code but it seems to me that you are not using it to do something.

I'm sure you're talking about this tag, not the one you use with the buttons.

I have a (maybe) useful idea, to overload you with work :p:

Why a Toast Message should not be a Custom View? So you could set some of its properties by Designer.
I know, it sounds strange, but for example Timers in VB.Net are "UI objects".


P.S. my cousin :D was writing his answer at the same time as mine.
[Note that my joke about the cousin is due to the question addressed directly to Erel]
 
Last edited:

udg

Expert
Licensed User
Longtime User
Hi @Erel, I couldn't find any reference to "mLbl.Font = DefaultFont".
Did you refer to the line "lbl.Font = DefaultTextFont" in the B4i section of sub ShowMessage?

My concern with mbase.Tag was because we set it when we Initialize the class, but once the fade out for the first message is over we set it to Null. Now a a new call to TMShow/ShowMessage will find a null Tag (instead of a reference to "Me") as it happened for the first call.
In my demo, ButtonX and ButtonP click events make use of a single Toast instance, while Button5 creates a new instance on every click.

Am I missing something?

TIA
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Did you refer to the line "lbl.Font = DefaultTextFont" in the B4i section of sub ShowMessage?
Yes.

Am I missing something?
The Tag is not needed if the variable that holds the class instance is a global variable. Only if it is a local variable.

The only case where it will be problematic is if you are using the same local instance multiple times.
 

LucaMs

Expert
Licensed User
Longtime User
The Tag is not needed
I still don't understand of which Tag you (both) are talking about.

Searched as text (in the @udg's project):

upload_2018-5-1_16-56-3-png.67418


mBase.Tag is not really used, I think.

b.Tag is just the tag of the buttons.

:eek:
 
Top