B4A Library [Class] Custom Toast Messages

Hello All,

Updated: [June 30th, 2012]. Added more functions to set options for a new ToastShow and added sound. All are
attached below. NOTE: The new one requires the Audio library and only two parameters to be passed! See Post #: 11
for more information.

Updated: [June 24th, 2012] and added support for 9 Patch Images. The original is unchanged and both are attached below.


Here is a custom toast message class. It allows a number of features over the standard toast message. Below is a list of what can be set in this custom toast message class:

The message. It will wrap the message if it is longer than one line.
The text size
The text color
The message background color if no image is used
The toast message background image
The vertical position
The horizontal position
The number of seconds to display
Can be closed immediately in code
Select auto close or close by Tapping the message

Uses the new Reflection library 2.20.
Version 1.0 is attached to this post with Demo Code.

Margret

Erel the new B4A classes are awesome!!:)
 

Attachments

  • CToastClass.zip
    14.1 KB · Views: 1,769
  • CToast_9Patch.zip
    19.1 KB · Views: 1,088
  • CToast_9PatchSound.zip
    20.4 KB · Views: 1,133
Last edited:

AntonBrz

Member
Licensed User
Longtime User
Installing your library first time

Hello, I'm an older programmer here who started in machine language and am not really familiar with Visual basic or java. I'm learning to program B4A and getting good results after only a week, when I came across your Toast library, and believe it is the perfect solution to keep from cluttering my panels with labels, which I'd then change the text in. I didn't use toast messages before because I couldn't fine tune them, but with the abilities described, I could display short toast messages whenever needed. My problem is this. How exactly do you install this in your additional libraries? Step by step please. I read you needed reflection and installed that easily in my additional library folder. It had a text note inside the package which said "move the jar file etc to the additional libraries folder." I did and reflection shows up to be checked in the libraries. But i couldn't find a jar file or anything called "toast" in your unzip to put in the additional libraries directory. I want to install the newest version with sound. Excuse me for being a newbie to this. :sign0104:

Thank you for any response.
Anton
 

AntonBrz

Member
Licensed User
Longtime User
Not Reflection, these toast libraries....

The reflection library is here:
http://www.b4x.com/forum/additional...pdates/6767-reflection-library.html#post39256

Download that, unzip and copy it into your libraries folder. Then make sure it's selected in the libs tab of b4a.

Thank you but sorry. As I said above, I already got the reflection library installed and it works. What I need here is a description of how to install the files for this Toast library they're talking about here. I unzipped those files (Toast from here) and there don't seem to be any class files to install. Sorry. You have helped me before though, thanks to you I gave up using canvas and am making rapid progress with panels.
Anton
 

AntonBrz

Member
Licensed User
Longtime User
Added .bas module

Thank you Klaus, it works like a charm now and Margret's code is well documented. I have no trouble using it.
 

AntonBrz

Member
Licensed User
Longtime User
Timing the message - custom

One more question please. Now that I have the messages working, I'd like to ask if the seconds delay could be modified to show messages of less than a second. I know you'll say that's not long enough to read anything, but I'm an artist who also does short films. I want to create some interesting displays. As an imaginary example, have you seen the old Adam West retro Batman show, where messages would flash on screen, 'bam!" "POW!". I don't want to say Bam or pow, but this custom toast is perfect for showing brief images as well as brief messages. But if they stay on screen an entire second, they seem slow. I want to get over the effect that B4A apps are slow or clunky and that the effects created can seem fluid. Perhaps you could direct me to the java where i could change a value, so the minimum delay in this custom toast routine would be smaller than a second? It could be used for many things, including short animations like animated gifs. Much needed.

Thanks
 

margret

Well-Known Member
Licensed User
Longtime User
Hello,

In the CToast Class code look for this sub:
B4X:
Private Sub SetToast(Tset As Int)
   TTime.Enabled = False
   TTime.Initialize("TTime", (Tset * 1000))
   TTime.Enabled = True
End Sub

Now change it to this:
B4X:
Private Sub SetToast(Tset As Int)
   TTime.Enabled = False
   TTime.Initialize("TTime", Tset)
   TTime.Enabled = True
End Sub

Now you pass the time as milliseconds, for example: 500 would be a half second.
 

AntonBrz

Member
Licensed User
Longtime User
timing the Toast

Hello,

In the CToast Class code look for this sub:
B4X:
Private Sub SetToast(Tset As Int)
   TTime.Enabled = False
   TTime.Initialize("TTime", (Tset * 1000))
   TTime.Enabled = True
End Sub

Now change it to this:
B4X:
Private Sub SetToast(Tset As Int)
   TTime.Enabled = False
   TTime.Initialize("TTime", Tset)
   TTime.Enabled = True
End Sub

Now you pass the time as milliseconds, for example: 500 would be a half second.

Thank you for responding to this. This makes CToast even more useful.

AntonBrz
 

sorex

Expert
Licensed User
Longtime User
hello,

I was just going through the daily posts and my eye fell onto this thread.
Looks like an excellent project to replace the default toast but I had one question about the posted mod.

are the lines marked with !!! at the beginning not overkill since the first IF already gives you the result of the second check?

Not that it would be faster or anything but less code is less to interpret for those who like to know everything about what they add into their projects :)

B4X:
   If File.Exists(File.DirAssets, Timage) Then
         TMS2pan.SetBackgroundImage( LoadBitmap(File.DirAssets, Timage) )
      Else
         ' Nine-Patch extension
!!!         If File.Exists(File.DirAssets, Timage) = False Then
            '
            ' If imagefile was not found in DirAssets, then it is propably a NinePatch-file
            '
            ' Prerequisites to work properly:
            '   -file must be located in <project folder>\Objects\res\drawable
            '   -file must be "read only"
            '   -filename must not be longer than 8 characters
            '   -filename must be lowercase
            '   -last two characters of the filename must be ".9" (For example "picabc.9.png")
            '   -imagefile must have the 9patch padding information
            '
            Dim strNpFileName As String
            Dim p As Int
            p = Timage.IndexOf(".")
            If p>1 Then 
               ' To use the 9patch mechanism we need to correct the filnames
               strNpFileName = Timage.SubString2(0,p)
               Try 
                  Dim r As Reflector
                   Dim package As String
                   Dim id As Int
                   package =r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
                  id =r.GetStaticField( package & ".R$drawable", strNpFileName)
                   r.Target =r.GetContext
                   r.Target =r.RunMethod("getResources")
                   TMS2pan.Background =r.RunMethod2("getDrawable", id, "java.lang.int")
               Catch
                  Log("#-Error trying ToastMessageShow2 with NinePatch")
               End Try
            
            End If
!!!         End If
      End If
 

Jamie

Member
Licensed User
Longtime User
Why is it necessary to set width and height to -2 before using getheight and getwidth? I've been using a variation of this routine of yours in my own class and have crazy results if I don't set these up first like you did.

B4X:
Dim Height, Width As Int
   TMS2text.Text = Tmessage
   TMS2text.TextSize = Ttextsize
   TMS2text.Width = -2
   TMS2text.Height = -2
   DoEvents    
   TMS2.Target = TMS2text
   Width = TMS2.RunMethod("getWidth")
   Height = TMS2.RunMethod("getHeight")

BTW Thanks for the class. My class is very similar but uses an ontouchlistener within a specified view and within an event of that view. Your code examples have helped me through a couple issues.
:sign0089:
 

Jamie

Member
Licensed User
Longtime User
I was just curious. I searched and found the thread you mentioned. It works, so that's good enough for me.
Thanks
 

Informatix

Expert
Licensed User
Longtime User
Why is it necessary to set width and height to -2 before using getheight and getwidth? I've been using a variation of this routine of yours in my own class and have crazy results if I don't set these up first like you did.

B4X:
Dim Height, Width As Int
   TMS2text.Text = Tmessage
   TMS2text.TextSize = Ttextsize
   TMS2text.Width = -2
   TMS2text.Height = -2
   DoEvents    
   TMS2.Target = TMS2text
   Width = TMS2.RunMethod("getWidth")
   Height = TMS2.RunMethod("getHeight")

BTW Thanks for the class. My class is very similar but uses an ontouchlistener within a specified view and within an event of that view. Your code examples have helped me through a couple issues.
:sign0089:

-2 is the constant value for WRAP_CONTENT. Advantage: the size of your view is automatically computed to fit the contents. Disadvantage: width and height returns -2 and not the computed size. That's why you have to use the java functions getWidth and getHeight to know this size.
 

GMan

Well-Known Member
Licensed User
Longtime User
i will use this class also, but after implementing etc., the app causes an error when calling the toast (at this point):
B4X:
TMS2text.Width = -2

The error is:
An error has occ.....
TMS2text.Width = -2
java.lang.NullPointerException

What i did wrong (or not) ?
 

GMan

Well-Known Member
Licensed User
Longtime User
:BangHead:

SOLVED
 

GMan

Well-Known Member
Licensed User
Longtime User
But:
if i tapped the (i.e. Button) twice, the Toast did not disappear ?
 

synapse

Member
Licensed User
Longtime User
Fatal signal 11 (SIGSEGV)

I wondered if anyone had come across this error please? It occurs with CToast but not Toast, crashing the app randomly on 4.2.2 but not, seemingly, 2.3.6.
 

Tria

New Member
Licensed User
Longtime User
Problem with Click/LongClick

Hi

I wanted to use this great class in my project, but something funny started to happen if I put the CToast in the LongClick event. I have attached a small testproject made from Margrets example.

I will try to explain, whats going on below.

1, I have 1 layout loaded at activity_create. When I click the button (Click_event) the next layout i loaded. Clicking the panel on number 2 layout closes it. No prob.

2, In the LongClick event i have put the Ctoast, and if I hold the button long enough the Ctoast opens as i should.

3,The funny thing is if I Click/longClick the button for about 1 sec. both events fires and both the Ctoast and the 2.layout opens.

Can't figure this out. Hope someone else can.

Thanks in advance

Rolf
 

Attachments

  • CToast Test.zip
    20.5 KB · Views: 190
Top