B4A Library [B4X] BCToast - Cross platform custom toast message

BCToast is a custom "toast" message implementation based on BCTextEngine: https://www.b4x.com/android/forum/t...-bbcode-parser-rich-text-view.106207/#content

It requires BCTextEngine v1.65+.

java_Vr5tF2Iymf.png


Note that unlike B4A built-in toast message feature, this toast message can only be displayed from an Activity.

Usage example:
B4X:
Sub Globals
   Private toast As BCToast
End Sub

Sub Activity_Create(FirstTime As Boolean)
   toast.Initialize(Activity) 'Use Page1.RootPanel in B4i or MainForm.RootPane in B4J.
End Sub

Sub Activity_Click
   toast.Show($"The time now is [b]$Time{DateTime.Now}[/b]"$)
End Sub

Default duration is set to 3000 ms. It can be changed with the DurationMs field.
The text itself is highly customizable. See BCTextEngine for more information.

Tip: use the plain tag if you are showing error messages or any other unsanitized text:
B4X:
Toast.Show($"[plain]${LastException.Message}[/plain]"$)
Otherwise the text might include square brackets which will be treated as invalid bbcode.


V1.01 - Adds support for images.
New fields:
B4X:
Public PaddingSides As Int = 15dip
Public PaddingTopBottom As Int = 10dip
Public MaxHeight As Int = 100dip
Public VerticalCenterPercentage As Int = 85
 

Attachments

  • BCToast.b4xlib
    4.5 KB · Views: 1,545
Last edited:

Ilya G.

Active Member
Licensed User
Longtime User
How can I make toast with icon?

B4X:
Toast.Show("[img FileName=dberror.png width=50/] Ошибка соединения с сервером")


1590564008268.png
 

Shay

Well-Known Member
Licensed User
Longtime User
How do I change the font? tagging is not working, also css had no affect
B4X:
Private toastMsg As String = $"Example of using [font=italic]custom[/font] font. We can also [font=italic size=50]change its size.[/font]"$
        toast.Show (toastMsg)
 

Jorge M A

Well-Known Member
Licensed User
This works. Please note TextEngine.CustomFonts

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    End Sub

Sub Globals
    Private toast As BCToast
    Private xui As XUI
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    toast.Initialize(Activity)
    toast.BB1.TextEngine.Initialize(Activity)
    toast.BB1.TextEngine.CustomFonts.Put("italic",xui.CreateFont(Typeface.CreateNew(Typeface.DEFAULT, Typeface.STYLE_ITALIC), 10)) 'size not important
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_Click
    toast.Show($"The [font=italic] time now [/font]is [b]$Time{DateTime.Now}[/b]"$)
End Sub
 

Shay

Well-Known Member
Licensed User
Longtime User
Thanks, what is the equivalent command for Typeface in B4J (I am getting error for Typeface)
 

Jorge M A

Well-Known Member
Licensed User
There is no Typeface in B4J. You need CreateFont
B4J:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private Button1 As Button
    Private toast As BCToast
    Private xui As XUI
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
    toast.Initialize(MainForm.RootPane)
    toast.BB1.TextEngine.Initialize(MainForm.RootPane)
    toast.BB1.TextEngine.CustomFonts.Put("italic", xui.CreateFont( fx.CreateFont("arial", 10, False, True), 10))
   
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Button1_Click
    toast.Show($"The [font=italic] time now [/font]is [b]$Time{DateTime.Now}[/b]"$)
End Sub

EDIT: The "True" parameter means Italic=True
 

Attachments

  • B4J-BCToast.zip
    2.1 KB · Views: 494

Shay

Well-Known Member
Licensed User
Longtime User
Thanks it is working !! (just need to fix the italic to arial)
 

ludoprgrm

Member
Licensed User
Hi,

Is it possible to make the text scrollable, or handle the popup height (or size) according to the message text's length ?

I would use it in a B4A application.

Many thanks

Regards
 

ludoprgrm

Member
Licensed User
Hi,

Many thanks, I've discovered while I was searching how use the B4XLongTextTemplate. It solved my problem.

Thanks again,

Regards
 

klaus

Expert
Licensed User
Longtime User
... which means that it is compatible with B4A, B4i and B4J.
Are you sure?
For example:
[B4X] B4XDrawer - sliding drawer
Works only with B4A and B4i, but not with B4J !?

I remember that one day you said that if it works at least for two platforms [B4X] is OK.
But I don't remember the thread.

Anyway, I agree with your comment in the previous post.
[B4X] should work for all three platforms or, if there are any restrictions, it must clearly be mentioned.

EDIT: I'm afraid that I didn't read carefully enough the [B4X] B4XDrawer - sliding drawer thread where it is written that the B4J version was added.
 
Last edited:
Top