B4A Library [Class] Custom MsgBox

This class will allow you to create customized non-modal message boxes, you can change colors, fonts, add bitmaps to the background, use RichStrings, etc.

Requirements:

- B4A 2.xx
- RichString Lib
- StringUtils Lib
- Add the Class module "CustomMsgBox.bas" to your project

How to use:

Initialize the class like this for example:

Code:
Sub Globals

    Dim MyMsgBox As CustomMsgBox

End Sub

Then, create your custom message box:
Code:
'With Icon
myMsgBox.Initialize(Activity, Me, "Default", "H", 1, 95%x, 200dip, LoadBitmap(File.DirAssets, "WarningIcon.png"))

'Without Icon
myMsgBox.Initialize(Activity, Me, "Default", "H", 1, 95%x, 200dip, Null)

'Then add text to the Title and Body of the custom msgbox
MyMsgBox.Title.Text = "Custom MsgBox v1.0"

MyMsgBox.ShowMessage("Simple box with 1 button, horizontal")

...

'This routine will get the button tapped, in this case "Default" 
'is the name of the message box, you can name yours with whatever name you like
Sub Default_Click

    Msgbox(myMsgBox.ButtonSelected, "Button Pressed")
            
End Sub

That's it!!.

All the available methods are self explanatory.

UPDATE: Added parameter to align the message

ShowMessage(BoxMessage As String, Alignment As String)

Updated to Version 1.0.4 on Feb 15, 2013.

The attached sample shows one of the many ways it can be used.

Screen shots:

6jJaIl.png]Shot 1
c0Odjl.png]Shot 2

fdE9El.png]Shot3
9Xxwel.png]Shot 4
 

Attachments

Last edited:
Thanks, didn't know you can have undeclared variables and still be able to compile.

Bryan
 
I am getting the message

Code:
Parsing code.                           Error
Error parsing program.
Error description: Unknown type: richstring
Are you missing a library reference?
Occurred on line: 226
Public Sub ShowRichMessage(BoxMessage As RichString, Alignment As String)

I have StringUtils (version 1.02) ticked..
 
Hello, I'm looking for a modal dialogue box that will allow an EditText, along with Okay/Cancel buttons for single field editing.

Will this class allow me to do this?
 
Am I missing something?

The vars, Delta, Rate, Scale etc appear in red?

I added the FullCustom funcion (minus the click) and added the default_click function, but the app displays it briefly and closes?

I am trying to use this to stop users closing the app without using the app menu when they press the back button.

Malky
 
Hi,

i've got a problem.
Please try this:
show a messagebox (only an OK-Button) and then change to another app, for example ebay... change back to your app and try to press the Button (in my app its only a OK-Button). Nothing happens and the messagebox will still appear...

Am I right?
 
Hi,

i've got a problem.
Please try this:
show a messagebox (only an OK-Button) and then change to another app, for example ebay... change back to your app and try to press the Button (in my app its only a OK-Button). Nothing happens and the messagebox will still appear...

Am I right?


Did you find a solution to this problem Softmaster? This happens to me also. In my case I show the message at the end of a game. Every 5th or 6th game I show an add after the message. When I come back to the game the message box is stuck. I can't press the Ok button, I can only hit the back button to exit the game.
 
Edit the class module

Code:
Sub Class_Globals
...
Dim Visible As Boolean
End Sub

Public Sub Close_Box
    BackPanel.Visible = False
    Visible = False
End Sub

Public Sub ShowMessage(BoxMessage As String, Alignment As String)
...
Visible = True
End Sub

Public Sub ShowRichMessage(BoxMessage As RichString, Alignment As String)
....
Visible = True
End Sub

and use in the app
Code:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If myMsgBox1.Visible Then
        myMsgBox1.Close_Box
        Return True
    End If
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If myMsgBox1.Visible Then myMsgBox1.Close_Box
End Sub
 
Edit the class module

Code:
Sub Class_Globals
...
Dim Visible As Boolean
End Sub

Public Sub Close_Box
    BackPanel.Visible = False
    Visible = False
End Sub

Public Sub ShowMessage(BoxMessage As String, Alignment As String)
...
Visible = True
End Sub

Public Sub ShowRichMessage(BoxMessage As RichString, Alignment As String)
....
Visible = True
End Sub

and use in the app
Code:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
    If myMsgBox1.Visible Then
        myMsgBox1.Close_Box
        Return True
    End If
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
If myMsgBox1.Visible Then myMsgBox1.Close_Box
End Sub


Thanks!!!!!
 
Hi NJDude,

good work, but a small mistake in the class.

Very imortant!

Please remove the panel after finishing. Here my example.
Code:
Sub Class_Globals
   ...
   Private iBackPanel As Int
   Private myActivity as Activity
   ...
End Sub

Public Sub Initialize(...
               ...
               myActivity = Activity
               iBackPanel  = myActivity.NumberOfViews
               myActivity.AddView(BackPanel, 0dip, 0dip, 100%x, 100%y)
End Sub

' You finished the work here
Private Sub PressedButton_Touch(...
  ...
  myActivity.RemoveViewAt(iBackPanel)
  ...
End Sub

After that correction, no more problems with the activity.

Thanks for your good work and inspiration.

Best regards from Germany and a happy new year.

Roland
 
Small issue, When I use this class and make an orientation change I loose the title of the messagebox, any ideas on how to fix this?
 
Thanks for this fantastic library.
I would like to add to the title 3 or more rows of text, is there a way to give more space to the "title"?
Also, i would like to print/export my long text in the cmsgbox, with the background image too, is there any way to do this?
Thank you all.
 
Last edited:
Back
Top