Android Code Snippet Custom Toast Message


You can customize the toast message text with CSBuilder:

This code allows you to also change the background color and the toast position:
Sub ShowCustomToast(Text As Object, LongDuration As Boolean, BackgroundColor As Int)
   Dim ctxt As JavaObject
   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:
Sub Activity_Click
   ShowCustomToast("Testing...", True, Colors.Green)
   Dim cs As CSBuilder
   cs.Initialize.Color(Colors.Blue).Size(20).Append("Custom Toast").PopAll
   ShowCustomToast(cs, True, Colors.Red)
End Sub
Last edited:


Well-Known Member
Licensed User
Longtime User
On my Android 7.1.1 I get a square toast.
Is it possible to have rounded corners?

Walter Brunmueller

Licensed User
Longtime 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.
I used this code, it works well on Android 7, but it gives an error on Android 11:
main_showcustomtoast (java line: 409)
java.lang.RuntimeException: Object should first be initialized (View).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(
    at anywheresoftware.b4a.objects.ViewWrapper.setBackground(
    at com.test.main._showcustomtoast(
    at com.test.main._activity_create(
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(
    at com.test.main.afterFirstLayout(
    at com.test.main.access$000(
    at com.test.main$
    at android.os.Handler.handleCallback(
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at java.lang.reflect.Method.invoke(Native Method)


Licensed User
I don't know this is a bug or not, but however `getView` and `setView` are deprecated, but `setView` still working well. i try it in android 11,12 and 13, with target 33.
I am not encouraging anyone to use outdated method, I just shared my experience with you.
Good luck