Android Color Dialog Tutorial using Basic4Android

In this tutorial, I will introduce another kind of dialog, the Color Dialog. First, you need to create one button and named it Button1. Next, named your layout name as "Main". 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.  
  5.         Dim Button1 As Button
  6.         Dim bmp As Bitmap
  7. End Sub
I have initialized bmp as Bitmap because I wanted to have an icon to my ColorDialog. Here's the full code for this:
  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. End Sub
  14.  
  15. Sub Activity_Create(FirstTime As Boolean)
  16.         'Do not forget to load the layout file created with the visual designer. For example:
  17.         'Activity.LoadLayout("Layout1")
  18.         Activity.LoadLayout("Main")
  19.         bmp.InitializeSample(File.DirAssets, "bermoy.jpg",48,48)
  20.         Activity.Title = "Color Dialog - Lyndon Bermoy"
  21.        
  22. End Sub
  23.  
  24. Sub Activity_Resume
  25.  
  26. End Sub
  27.  
  28. Sub Activity_Pause (UserClosed As Boolean)
  29.  
  30. End Sub
  31.  
  32. Sub Button1_Click
  33.         Dim cd As ColorDialog
  34.         Dim color As Int
  35.         Dim ret As Object
  36.         cd.RGB = Colors.RGB(128, 128, 128)
  37.         ret = cd.Show("B4A Color Dialog", "Yes", "No", "Maybe", bmp)   
  38.         ToastMessageShow(ret & " : " & cd.Red & " " & cd.Green & " " & cd.Blue, False)
  39.         Activity.color = cd.RGB
  40. End Sub
Note: cd - used as my variable for ColorDialog color - used as my variable for Int ret - used as my variable for Object cd.RGB = Colors.RGB(128, 128, 128)- a syntax used to declare an RGB Color (Red,Blue,Green). 128 is the default color for Red, as well as the Green and color Blue. Activity.color = cd.RGB - syntax used that after clicking the button, it will set the RGB color you set and it will be the background color. 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