Android Code Snippet ProgressBar continues until Sub complete

Devan

Member
Licensed User
The below ProgressBar code will help to continually loop the ProgressBar until the Sub is complete its job. Hopefully this can be useful. Thank you to Erel.


Sub Process Global
Dim t1 As Timer
Dim Progress As Int = 0
End Sub

Sub Global
Private pb As ProgressBar
End Sub

B4X:
Sub EmailBtn4_Click
      
        'ProgressBar need to be Initialize
        pb.Initialize("pb")
        Log(pb)
        t1.Enabled = True
        Log (t1)
      
           Dim gd As GradientDrawable
               gd.Initialize("TOP_BOTTOM", Array As Int(Colors.Blue, Colors.Red))
                gd.CornerRadius = 3dip
         
        'Background colour Yellow
        Dim cd As ColorDrawable
               cd.Initialize(Colors.Yellow, 3dip)
               SetProgressDrawable(pb, gd, cd)

            pb.Progress = 1
            Log(pb.Progress)
      
        'Starts the timer to do the ProgressBar Loop.
        t1_tick
               
        Activity.AddView(pb, 10dip, 10dip, 400dip, 50dip)
           Log(pb)

      
      
            SMTP.Initialize("mail.xxxx.com.my", 587, "email", "password", "SMTP")
            'SMTP.Initialize("mail.xxxx.com.my", 587, "email", "", "SMTP")
          
            SMTP.StartTLSMode = False
          
              SMTP.To.Add("email1")
            'SMTP.To.Add("email2")
          
            SMTP.Subject = "Email Title "
            SMTP.Body = "Message "
            SMTP.AddAttachment(File.DirInternal, "file.db")
            SMTP.AddAttachment(File.DirInternal, "file.CSV")
            SMTP.AddAttachment(File.DirInternal, ".xls")
            SMTP.Send
          
        Log(pb)
End Sub  

'This below Sub SetProgressDrawable code was given by Erel
Sub SetProgressDrawable(p As ProgressBar, drawable As Object, backgroundDrawable As Object)
   Dim r As Reflector
   Dim clipDrawable As Object
   clipDrawable = r.CreateObject2("android.graphics.drawable.ClipDrawable", _
      Array As Object(drawable, Gravity.LEFT, 1), _
      Array As String("android.graphics.drawable.Drawable", "java.lang.int", "java.lang.int"))
   r.Target = p
   r.Target = r.RunMethod("getProgressDrawable") 'Gets the layerDrawable
   r.RunMethod4("setDrawableByLayerId", _
      Array As Object(16908288, backgroundDrawable), _
      Array As String("java.lang.int", "android.graphics.drawable.Drawable"))
   r.RunMethod4("setDrawableByLayerId", _
      Array As Object(r.GetStaticField("android.R$id", "progress"), clipDrawable), _
      Array As String("java.lang.int", "android.graphics.drawable.Drawable"))
End Sub

'Starts the timer to do the ProgressBar Loop. Once Send email button clicked.
Sub t1_tick
       
               'Progress < 100. The ProgressBar lenght is 100.
            If Progress< 100 Then
                    Progress = Progress + 1
                    Log (Progress)
                    pb.Progress = Progress
                    Log (pb.Progress)
                Else
                    'To re-Set the ProgressBar to 0. sometimes it gets late to send email. Until then the
                    'ProgressBar will be running from 0 to 100 continually.
                    Progress = 0
                    Log (Progress)
                    pb.Progress = Progress
              End If
     
        Log (pb.Progress = Progress)
        Log (Progress)
End Sub

Sub SMTP_MessageSent(Success As Boolean)
      
    Log(Success)
    If Success Then
        ToastMessageShow("Message sent successfully", True)
        Progress = 0
    Else
        ToastMessageShow("Error sending message", True)
        Log(LastException.Message)
    End If
  
    t1.Enabled = False
    Log (t1)
    pb.RemoveView
    Log(pb)
  
End Sub
 
Top