Android Code Snippet Custom Toast Message

Erel

Administrator
Staff member
Licensed User


You can customize the toast message text with CSBuilder: https://www.b4x.com/android/forum/threads/76226/#content

This code allows you to also change the background color and the toast position:
B4X:
Sub ShowCustomToast(Text As Object, LongDuration As Boolean, BackgroundColor As Int)
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim duration As Int
   If LongDuration Then duration = 1 Else duration = 0
   Dim toast As JavaObject
   toast = toast.InitializeStatic("android.widget.Toast").RunMethod("makeText", Array(ctxt, Text, duration))
   Dim v As View = toast.RunMethod("getView", Null)
  Dim cd As ColorDrawable
  cd.Initialize(BackgroundColor, 20dip)
  v.Background = cd
   'uncomment to show toast in the center:
'   toast.RunMethod("setGravity", Array( _
'       Bit.Or(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL), 0, 0))
   toast.RunMethod("show", Null)
End Sub
Depends on JavaObject library

Usage example:
B4X:
Sub Activity_Click
   ShowCustomToast("Testing...", True, Colors.Green)
   Sleep(3000)
   Dim cs As CSBuilder
   cs.Initialize.Color(Colors.Blue).Size(20).Append("Custom Toast").PopAll
   ShowCustomToast(cs, True, Colors.Red)
End Sub
 
Last edited:

Walter Brunmueller

Member
Licensed User
I had problems to get a toast message.
Afte hours of testing I found the solution.
It is necessary to activate notification for your app in the android system.

I hope this helps.
 
Top