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

Erel

Administrator
Staff member
Licensed User
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+.



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.

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

Last edited:

GanjaKyp

Active Member
Licensed 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
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

Active 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
Thanks, what is the equivalent command for Typeface in B4J (I am getting error for Typeface)
 

Jorge M A

Active 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

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
 
Top