Android Custom Dialog Tutorial using Basic4Android

Hi! I will introduce another tutorial in Dialogs using Basic4Android, the Custom Dialog. A custom dialog refers to be in the customization of your dialog that you will going to create on your own. First, you need to create two buttons and named it Button1 and btn1, and also one label and named it as lbl1. Next, named your layout name as "Main". Follow the image below for the abstract design. image1 Declare first your variable in the Sub_Global like this:
  1. Sub Globals
  2.     'These global variables will be redeclared each time the activity is created.
  3.     'These variables can only be accessed from this module.
  4.     Dim Button1 As Button
  5.     Dim bmp As Bitmap
  6.     Dim lbl1 As Label
  7.     Dim btn1 As Button
  8.     End Sub

Note:

bmp is our variable for Bitmap because I wanted to have an icon to my Custom Dialog. lbl1 our variable for Label. Button1 - our variable for Button. btn1 - our 2nd variable for our Button. Here's the full code for this tutorial:
  1.     Sub Process_Globals
  2.     'These global variables will be declared once when the application starts.
  3.     'These variables can be accessed from all modules.
  4.      
  5.     End Sub
  6.      
  7.     Sub Globals
  8.     'These global variables will be redeclared each time the activity is created.
  9.     'These variables can only be accessed from this module.
  10.      
  11.     Dim Button1 As Button
  12.     Dim bmp As Bitmap
  13.     Dim lbl1 As Label
  14.      
  15.     End Sub
  16.      
  17.     Sub Activity_Create(FirstTime As Boolean)
  18.     'Do not forget to load the layout file created with the visual designer. For example:
  19.     'Activity.LoadLayout("Layout1")
  20.     Activity.LoadLayout("Main")
  21.     bmp.InitializeSample(File.DirAssets, "bermoy.jpg",48,48)
  22.     Activity.Title = "Custom Dialog - Lyndon Bermoy"
  23.      
  24.     End Sub
  25.      
  26.     Sub Activity_Resume
  27.      
  28.     End Sub
  29.      
  30.     Sub Activity_Pause (UserClosed As Boolean)
  31.      
  32.     End Sub
  33.      
  34.     Sub Button1_Click
  35.     Dim cd As CustomDialog
  36.     Dim pnl As Panel 'we will initialize Panel to put our CustomDialog there
  37.     pnl.Initialize("pnl")
  38.     Dim bgnd As ColorDrawable 'bgnd is the variable for the background
  39.     bgnd.Initialize(Colors.Green, 5dip) 'we initialized background color as Green
  40.     pnl.Background = bgnd 'the panel is now equal to the background color as Green
  41.     Dim btn1 As Button
  42.     btn1.Initialize("btn1")
  43.     btn1.Text = "Press me"
  44.     pnl.AddView(btn1, 80dip, 50dip, 60dip, 60dip) 'size of our panel
  45.     lbl1.Initialize("")
  46.     pnl.AddView(lbl1, 50dip, 120dip, 120dip, 60dip)
  47.     cd.AddView(pnl, 5%x, 0%y, 77%x, 70%y) 'sizing relative to the screen size is probably best
  48.     ret = cd.Show("B4A Custom Dialog", "Yes", "No", "Maybe", bmp)
  49.     End Sub
  50.  
  51.     Sub btn1_Click
  52.     lbl1.Text = "Pressed!"
  53.     lbl1.Color = Colors.Black
  54.     ToastMessageShow("Pressed!", False)
  55.     End Sub

Note:

cd - our variable used for Custom Dialog pnl - we will initialize Panel to put our CustomDialog there bgnd - bgnd is the variable for the background color bgnd.Initialize(Colors.Green, 5dip) - we initialized background color as Green, 5dip here is the corner radius of the background color. pnl.Background = bgnd - 'the panel is now equal to the background color as Green btn1.Text = "Press me" - if our Button1 is clicked then the btn1 text will be equal to "Press me" pnl.AddView(btn1, 80dip, 50dip, 60dip, 60dip) - it will add a view to the panel with btn1 there, 80dip as size of the left view, 50dip size of the top view, 60dip as the height, and 60dip as the width of the panel. lbl1.Initialize("") - we initialize that label1 is said to be blank. pnl.AddView(lbl1, 50dip, 120dip, 120dip, 60dip) - it will add a view to the panel with lbl1 there, 50dip as size of the left view, 120dip size of the top view, 120dip as the height, and 60dip as the width of the panel. cd.AddView(pnl, 5%x, 0%y, 77%x, 70%y) - sizing relative to the screen size is probably best. It adds a custom layout view, most probably a Panel to the Custom Dialog, 5%x as size of the left view, 0%y size of the top view, 77%x as the height, and 70%y as the width of the panel. ret = cd.Show("B4A Custom Dialog", "Yes", "No", "Maybe", bmp) - this syntax will trigger the Custom Dialog to show and choose from Yes, No, or Maybe.
  1. Sub btn1_Click
  2.     lbl1.Text = "Pressed!"
  3.     lbl1.Color = Colors.Black
  4.     ToastMessageShow("Pressed!", False)
  5.     End Sub
The code above means that by clicking our btn1, the lbl1 label will turned out to be displayed to "Pressed" and it will have a black color, and also it will have a msgbox that will display "Pressed". Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment