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,874
  • CToast_9Patch.zip
    19.1 KB · Views: 1,165
  • CToast_9PatchSound.zip
    20.4 KB · Views: 1,228
Last edited:

margret

Well-Known Member
Licensed User
Longtime User
Hello,

All the code is there and you should be able to change it to whatever you are looking for to work with the NinePatchImages. I have not used them yet. Once you include the NinePatch code, there is only one line of code that you need to change in the current class. It is line 65 listed below:

B4X:
TMS2pan.SetBackgroundImage(LoadBitmap(File.DirAssets, Timage))
 

fredo

Well-Known Member
Licensed User
Longtime User
OK, thanks for the hint, margret.

I've tried my very best, and with help of Erel's routine I have the following solution working:

I deleted Line 65:
B4X:
TMS2pan.SetBackgroundImage( LoadBitmap(File.DirAssets, Timage) )

And inserted the following block at line 65:

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

This works so far on my device - see attachment.

The ninepatch image I used to test is also attached.

Regards,
fredo
 

Attachments

  • CToast1.png
    CToast1.png
    84 KB · Views: 1,409
  • tmback.9.png
    tmback.9.png
    4.4 KB · Views: 2,270

margret

Well-Known Member
Licensed User
Longtime User
Thanks to fredo:D

He brought up the idea of using my class with the 9 Patch images. I have added the code to support the 9 Patch images for those who wish to do so. It is attached to the first post to download. I left the original unchanged and it too is attached to the first post.

Thanks,

Margret:)
 
Last edited:

margret

Well-Known Member
Licensed User
Longtime User
* * * ANY INTEREST * * *

Is anyone interested in me adding the code to the class that will produce a tone or beep when the toastmessage shows? If so I will add this. Also, I thought about expanding the class where you can call it with just the message and the time. The other parameters can be set with other functions at the beginning and/or changed later in the code. If you are interested, let me know and I will work on it.

Margret
 

splatt

Active Member
Licensed User
Longtime User
Good Idea!

* * * ANY INTEREST * * *

Is anyone interested in me adding the code to the class that will produce a tone or beep when the toastmessage shows?

Margret

I think that would be useful, because if you are interacting with the screen, it is easy for a Toast message to be obscured by your hand.
 

margret

Well-Known Member
Licensed User
Longtime User
Attached to the first post is a new Toast Message class that now support 9 Patch images and Sound. Added new functions for setting the new ToastShow function that now only requires passing two parameters!

These new functions to setup the ToastShow can be used at anytime in the code after the Initialize command has been called. This way you can change anyone of, or all options about the way the ToastShow is displayed and still only have to pass two parameters.

Margret
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
Happy 4th to Everyone!

Also, looking for any feedback of issues if any have been found. I was thinking about maybe adding TTS to the Toast but didn't know if that would be used by anyone. Next up is a [DataBase Class]!

Anyway Happy 4th. :D
Happy 4th! Though, here in Greece, we're ready to celebrate a happy 4th default :)
 

tremara1

Active Member
Licensed User
Longtime User
database class

mmmmmmmmmm database class.......yes please.
 

metrick

Active Member
Licensed User
Longtime User
Wait for it for so long! Already in my app and working great.
Thanks so much, Margret.
 

kalarius

Active Member
Licensed User
Longtime User
hi,
I copy the example into my project.
(I place the intiliazation code and the module in my code)

on the screen play a video over and over again

I use the
MyT.ToastMessageShow2("this is my message", 3,50, 50, "",Colors.red , Colors.White, 28, True,False)

but only a black line shows me on the screen

the toastmessageshow("This is my message",false) shows the message ok
(I use a timer to display the message every 5seconds)

Activity.LoadLayout("main") The main is just a black screen I do not use it into my code


Kalarakis Michalis
 

bsnqt

Active Member
Licensed User
Longtime User
@margret

I know that you (or somebody) mentioned that it is not a "real" toast message, as it can show only inside the application (in-app toast message).

But your toastmessages are so nice, so I am very impressive and I just wonder how can we make it "natural" like the real toastmessage? (i.e. so that it could show itself even when the application goes to the background, or it even can show during the calling screen, for example).

Thanks

bsnqt
 
Top