Android Progressbar Tutorial using Basic4Android

Hi! In this tutorial, I will introduce another component/controls in Basic4Android, the ProgressBar. ProgressBar displays a bar to the user representing how far the operation has progressed; the application can change the amount of progress modifying the length of the bar as it moves forward. On this, you need to create one ProgressBar and named it as "pgb1", two Labels and named it as "lblPercent" for the percentage, and "Label1" and label it as "%". Next, named your abstract design as "tutorial". Your abstract designer will be like this one below: imageprogress Declare 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 timer1 As Timer
  5.         Dim pgb1 As ProgressBar
  6.         Dim num As Int
  7.         Dim lblPercent As Label
  8. End Sub

Note:

timer1 - our variable used for the Timer. We need timer to determine the increasing progress of our ProgressBar. Timer schedules one-shot or recurring tasks for execution. pgb1 - our variable for our ProgressBar. num - we used num as an integer to increment the value of the ProgressBar that complements in time. lblPercent - this is our variable for our Label to display the number in percentage of the ProgressBar. Next, Declare the Timer and its method name Tick.
  1. Sub timer1_Tick
  2.         num = num + 1
  3.         pgb1.Progress = num
  4.         lblPercent.Text = num
  5.         If num > 100 Then
  6.         timer1.Enabled=False
  7.         Msgbox("","Completed")
  8.         pgb1.Visible=False
  9.         End If
  10. End Sub

Note:

timer1_Tick - timer1 here is our control and its method name is Tick in which it defines that the timer will now start. num = num + 1 - num is declared to increment its value by 1. pgb1.Progress = num - the value of our progressbar will be equal to the incrementation of our num. lblPercent.Text = num - the value of our lblPercent as Label will be equal to the incrementation of our num in number.
  1. If num > 100 Then
  2.         timer1.Enabled=False
  3.         Msgbox("","Completed")
  4.         pgb1.Visible=False
  5. End If

Note:

If our variable num will reach at 99, then our timer will be disabled and it will display "Completed". Also, our ProgressBar will be vanished. Now, We will create our Activity_Create:
  1. Sub Activity_Create(FirstTime As Boolean)
  2.         'Do not forget to load the layout file created with the visual designer. For example:
  3.         Activity.LoadLayout("tutorial")
  4.         timer1.Initialize("timer1",400)
  5.         timer1.Enabled=True
  6. End Sub

Note:

Activity.LoadLayout("tutorial") - the named tutorial here is the design in which we created in the abstract designer. timer1.Initialize("timer1",400) - we initialized our timer as the named of timer1 and the 400 here is the time interval in milliseconds. timer1.Enabled=True - we initialized our timer to be enabled to perform its action. Here's the full code of 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.         Dim timer1 As Timer
  11.         Dim pgb1 As ProgressBar
  12.         Dim num As Int
  13.         Dim lblPercent As Label
  14. End Sub
  15.  
  16. Sub Activity_Create(FirstTime As Boolean)
  17.         'Do not forget to load the layout file created with the visual designer. For example:
  18.         Activity.LoadLayout("tutorial")
  19.         timer1.Initialize("timer1",400)
  20.         timer1.Enabled=True
  21. End Sub
  22. Sub timer1_Tick
  23.         num = num + 1
  24.         pgb1.Progress = num
  25.         lblPercent.Text = num
  26.         If num > 100 Then
  27.         timer1.Enabled=False
  28.         Msgbox("","Completed")
  29.         pgb1.Visible=False
  30.         End If
  31. End Sub
  32. Sub Activity_Resume
  33. End Sub
  34.  
  35. Sub Activity_Pause (UserClosed As Boolean)
  36. End Sub
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer STI College - Surigao City Mobile: 09126450702 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment