Android Question Custom Toast Message failing

Robert Valentino

Well-Known Member
Licensed User
Longtime User
After upgrading to B4A 10.9 and switching to SDK 30

My Custom Toast Message (CTM) has started failing

The toast.RunMethod("getView", Null) is returning v as NOT IsInitialized


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)
#if Debug
            If     v.IsInitialized = False Then
                Log("v NOT Initialized")
            End If            
#End If

  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

Any ideas how to fix this?
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Just found this on a websearch

B4X:
 public View getView (). This method was deprecated in API level 30


Means changing a lot of call. But I guess I will have to
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I use B4XPages for most everything and this is what I did

in the MAIN I did the following

B4X:
Sub Globals
   
    Private mToast                                As BCToast
End Sub


Public  Sub Get_Toast As BCToast
            Return mToast
End Sub

Private Sub Activity_Create(FirstTime As Boolean)
            Log($"${CRLF} ${CRLF} ${CRLF}BBs-Startup${CRLF} "$)
   
            MakeToast                                   
End Sub

Private Sub MakeToast
            'bc.Initialize(B4XPages.GetNativeParent(Me))   
           
            Log("Main::MakeToast -  ItalicMonospaceSupported:" &cBBsGlobals.gItalicMonospaceSupported)
           
            Dim xui         As XUI
           
            '-----------------------------------------------------------------------------------------------------------------------
            '    Some OLD Device do not support BOLD_ITALIC with MonoSpace
            '-----------------------------------------------------------------------------------------------------------------------
            If  cBBsGlobals.gItalicMonospaceSupported Then
                Dim MonoFont    As B4XFont = xui.CreateFont(Typeface.CreateNew(Typeface.MONOSPACE, Typeface.STYLE_BOLD_ITALIC), 16)
            Else
                Dim MonoFont    As B4XFont = xui.CreateFont(Typeface.CreateNew(Typeface.MONOSPACE, Typeface.STYLE_BOLD), 16)               
            End If
           
            mToast.Initialize(Activity)                       
            mToast.BB1.TextEngine.Initialize(Activity)

            mToast.BB1.TextEngine.CustomFonts.Put("msf", MonoFont)    '
End Sub

Then I have have a routine in a code class (cGenFuncs)
B4X:
Public  Sub MyToastMessageShow(xText As Object, xLongDuration As Boolean, xLocation As Int, xBackgroundColor As Int, xTextColor As Int, xAlert As Boolean)
            Dim toast         As BCToast    = CallSub(Main, "Get_Toast")      ' get the Toast Routine from Main
           
            If  xLongDuration Then
                toast.DurationMs = 3000
            Else
                toast.DurationMs = 6000
            End If
           
            toast.PaddingSides        = 10dip
            toast.PaddingTopBottom    = 15dip
            toast.pnl.SetColorAndBorder(xBackgroundColor, 2dip, xTextColor, 5dip)
            toast.DefaultTextColor    = xTextColor    

           
            Select     xLocation
                    Case     Gravity.Top
                             toast.VerticalCenterPercentage    = 15

                    Case     Gravity.CENTER, Gravity.CENTER_VERTICAL, Gravity.NO_GRAVITY
                            toast.VerticalCenterPercentage    = 50
                           
                    Case     Gravity.BOTTOM                       
                             toast.VerticalCenterPercentage    = 85                       
                           
                    Case    Else
                             toast.VerticalCenterPercentage    = 85                                               
            End Select

            toast.Show($"[font=msf size=16][plain]${xText}[/plain][/font]"$)
                       
            If  xAlert Then
                mAlertBeeper.Beep
            End If
End Sub

I just do a cGenFuncs.MyToastMessage(...)

I am sure there are better ways to do this but this is working for me.
 
Upvote 0
Top