Android Question show progress or loading indicator on long process

phukol

Active Member
Licensed User
Longtime User
Hi guys!

Im trying to show a loading indicator on my sub activity_Create however, when i encounter a long loading process (checking for connection, different information) the loading indicator pauses and only resumes after a certain period. How can i "background" load my app while showing the animation in front? Should i use thread? How can i possible execute it? Timer is a no go because even timer is not executed until my "background" processes are finished.

here is my current app behavior :

 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,
A quick reply without having seen any code : I guess you are doing your connection in the Activity's code, reason why the progress dialog is not updated at some times.
Using a separated thread will fix of course. But you could simply try using a service for the connection and updates too. You could try adding DoEvents in your long loop if any (but I remember it was not "prefered" by Erel).
 
Upvote 0

phukol

Active Member
Licensed User
Longtime User
Thank you lemonisdead!

I already thought of those approaches however

a.) doevents is not working i already placed them in key areas but its not working (im not using a loop btw)
b) i have no freaking idea how can i implment a thread for displaying a loading animation

btw here is my code for activity create:

B4X:
'prepare update
    Dim strMaxID As String = "0"
     Starter.c = Starter.s.ExecQuery("select exm_id from tbl_exam order by exm_id desc limit 1")
    If Starter.c.RowCount > 0 Then 
        Starter.c.Position = 0
        strMaxID = Starter.c.GetString("exm_id") 
    End If
   
    
    Dim dd As ServiceData
    dd.EventName = "dd"
    dd.Target = Me
    dd.StringData =  "4,1," & strMaxID
    CallSubDelayed2(ReviewAmigo, "processService", dd)
       
   
   
    Activity.Color = Colors.White
    Dim imgBack As ImageView
    imgBack.Initialize("")
    imgBack.Bitmap = LoadBitmap(File.DirAssets,"study_anywhere.jpg")
    imgBack.Gravity = Gravity.FILL
           
    Activity.AddView(imgBack, 0, 0, 100%x, 100%y )
   
    If pnlProgress.IsInitialized = False Then
        pnlProgress.Initialize("pnlProgress")
        pnlProgress.Color = Colors.ARGB(0,21,21,21)
        
        pnlLoader.Initialize("pnlLoader")
        pnlLoader.Color = Colors.ARGB(200,255,0,0)
        Dim cd As ColorDrawable
           cd.Initialize2(Colors.ARGB(200,255,0,0),3dip,1dip,Colors.darkGray)
        pnlLoader.Background = cd
       
        li1.Initialize(22)
        li1.IndicatorColor = Colors.ARGB(200,255,255,255) 
       
        pnlLoader.AddView(li1,10dip ,10dip,80dip,80dip) 
        pnlProgress.AddView(pnlLoader,(100%x/2) - 50dip  ,(100%y/2) - 50dip ,100dip,100dip) 
        Activity.AddView(pnlProgress,0 ,0 ,100%x,100%y)
    End If
    If pnlProgress.IsInitialized Then pnlProgress.BringToFront 
   
    'DoEvents
    showProgress(True)
   
    'Activity Specific
    Activity.LoadLayout("main_page")
    pnlMain.LoadLayout("main_page_info")
    scv1.Panel.LoadLayout("main_page_menu")
   
    ActionBar.SetAsActionBar  
    ActionBar.TitleTextColor = Colors.White
    ActionBar.Color =Colors.RGB(255,152,0)
    ActionBar.Title = "Review Amigo"
    ActionBar.SubTitleTextColor =Colors.White
    ActionBar.SubTitle =""
    ActionBar.InitMenuListener
    bd = xml.GetDrawable("ic_menu_white_24dp")
    ActionBar.NavigationIconDrawable = bd
    ActionBar.InitMenuListener

      DoEvents
    Dim strSQL As String = "select * from tbl_user limit 1"    
    Starter.c = Starter.s.ExecQuery(strSQL)
    If     Starter.c.RowCount > 0 Then
        Starter.c.Position = 0  
         Starter.oEmail = Starter.c.GetString("u_email")
         Starter.oLName = Starter.c.GetString("u_lname")
         Starter.oFName = Starter.c.GetString("u_fname")
    Else
        Starter.oEmail = ""
        Starter.oLName = "dela Cruz"
        Starter.oFName = "Juan"
    End If
   
    DoEvents
    If File.Exists(File.DirDefaultExternal,"pic.png") = True Then 
        Starter.bmp1 = LoadBitmap(File.DirDefaultExternal,"pic.png")
    Else
        Starter.bmp1 = LoadBitmap(File.DirAssets,"profile3.jpg")
       End If
    
     DoEvents
    If Utils.Connected Then
        If Common.Facebook.Connected Then 
            DoEvents
            Dim dd As ServiceData
            dd.EventName = "dd"
            dd.Target = Me
            dd.StringData =  "http://graph.facebook.com/" & Starter.fbID & "/picture?width=100&height=100"
           
            Dim job As HTTPJob
            job.Initialize("j", Me)
            Dim tag As SJTag
            tag.Initialize
            tag.data = dd
            job.tag = tag
            job.Download("http://graph.facebook.com/" & Starter.fbID & "/picture?width=100&height=100")
            
        Else
            
            PDB1.Initialize(Starter.bmp1)
            MSP1.withName(Starter.oFName & " " & Starter.oLName).withEmail("Future Nurse").withIcon(PDB1)
            builDrawer
        End If
        
    Else
        
        PDB1.Initialize(Starter.bmp1)
        MSP1.withName(Starter.oFName & " " & Starter.oLName).withEmail("Future Nurse").withIcon(PDB1)
        builDrawer
    End If
     
     DoEvents
    
    'main page menu
    btn1.Typeface = Starter.fa
    btn1.Text = Chr(0xf044)
   
    btn2.Typeface = Starter.fa
    btn2.Text = Chr(0xf096)
   
    btn3.Typeface = Starter.fa
    btn3.Text = Chr(0xf280)
   
    btn4.Typeface = Starter.fa
    btn4.Text = Chr(0xf02e)
   
    btn5.Typeface = Starter.fa
    btn5.Text = Chr(0xf080)
   
    btn6.Typeface = Starter.fa
    btn6.Text = Chr(0xf013)
   
    scv1.Panel.Height = btn6.Top + btn6.Height + 100dip
    If pnlProgress.IsInitialized Then pnlProgress.BringToFront
 
Upvote 0

phukol

Active Member
Licensed User
Longtime User
Yep thank you lemonisdead,

ive managed to fix it by trying to use this code

B4X:
Sub Globals
  
    Dim ThreadTest As Thread
  
    ThreadTest.Initialise("TT")
  
    ThreadTest.Start(Null,"TestThread",Null)
  
End Sub

then inside the thread i just called my loading sub
B4X:
Sub TestThread

    showProgress(True)

End Sub

However under Release mode im experiencing this error:

java.lang.RuntimeException: Thread.Start : Sub testthread not found!

its working in Debug mode but not in Release mode
 
Last edited:
Upvote 0
Top