Android Tutorial Change color of Android ProgressBar

moster67

Expert
Licensed User
it's true but I am more interested in changing the colour of the progressbar :)
 

Erel

Administrator
Staff member
Licensed User
Wasn't easy but here it is:



Depends on the reflection library.
B4X:
Sub Globals
   Dim pb As ProgressBar
End Sub
Sub Activity_Create(FirstTime As Boolean)
   pb.Initialize("pb")
   Dim gd As GradientDrawable
   gd.Initialize("TOP_BOTTOM", Array As Int(Colors.Blue, Colors.Red))
        gd.CornerRadius = 3dip
   SetProgressDrawable(pb, gd)

   pb.Progress = 50
   Activity.AddView(pb, 10dip, 10dip, 300dip, 50dip)

End Sub

Sub SetProgressDrawable(p As ProgressBar, drawable 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(r.GetStaticField("android.R$id", "progress"), clipDrawable), _
      Array As String("java.lang.int", "android.graphics.drawable.Drawable"))
End Sub
 

moster67

Expert
Licensed User
Wow. Your support and knowledge is amazing. Thanks.

Can't test now but will do so this evening.

Sent from my HTC HD2
 

Erel

Administrator
Staff member
Licensed User


Here:
B4X:
Sub Globals
   Dim pb As ProgressBar
End Sub
Sub Activity_Create(FirstTime As Boolean)
   pb.Initialize("pb")
   Dim gd As GradientDrawable
   gd.Initialize("TOP_BOTTOM", Array As Int(Colors.Blue, Colors.Red))
        gd.CornerRadius = 3dip
   Dim cd As ColorDrawable
   cd.Initialize(Colors.Yellow, 3dip)
   SetProgressDrawable(pb, gd, cd)

   pb.Progress = 50
   Activity.AddView(pb, 10dip, 10dip, 300dip, 50dip)

End Sub

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
Note that this solution uses an undocumented value.
 

Jamsa

Member
Licensed User
I've used the code, but it doesn't work 100 %.

I use a progressbar that has to update from time to time, but once it changes, the progressbar colors dissappears.

My code:
B4X:
Sub InitUserTab
   pbCatRating.Initialize("pbCatRating")
   pbCatPaidFree.Initialize("pbCatPaidFree")
   pbMaturety.Initialize("pbMaturety")
   
   lblMaturety.Initialize("lblMaturety")
   lblCatRating.Initialize("lblCatRating")
   lblCatPaidFree.Initialize("lblCatPaidFree")
   
   pTabUser.AddView(pbCatRating, 140, 10, 160, 30)
   pTabUser.AddView(pbCatPaidFree, 140, 70, 160, 30)
   pTabUser.AddView(pbMaturety, 140, 130, 160, 30)
   pTabUser.AddView(lblMaturety, 10, 130, 130, 40)
   pTabUser.AddView(lblCatRating, 10, 10, 130, 40)
   pTabUser.AddView(lblCatPaidFree, 10, 70, 130, 40)
End Sub

Sub UpdateUserTab(UserRating As Int)
   Dim gd As GradientDrawable
   Dim cd As ColorDrawable
   
   gd.Initialize("TOP_BOTTOM", Array As Int(Colors.Red, Colors.White))
   gd.CornerRadius = 30dip
   cd.Initialize(Colors.Transparent, 30dip)    
   
   SetProgressDrawable(pbCatRating, gd)
   SetProgressDrawable(pbCatPaidFree, gd)
   SetProgressDrawable(pbMaturety, gd)
   
   lblCatRating.Text = "Kategori - rating:"
   pbCatRating.Tag = 0
   If UserRating > 0 Then
      pbCatRating.Progress = 100 / UserRating
   Else
      pbCatRating.Progress = 0
   End If
   'pbCatRating.Invalidate
   
   lblCatPaidFree.Text = "Kategori/betalt vs gratis - downloads:"
   pbCatPaidFree.Tag = 0
   pbCatPaidFree.Progress = 2 * 20
   
   lblMaturety.Text = "Modenhed/tilgængelighed:"
   pbMaturety.Tag = 0
   pbMaturety.Progress = 3 * 20
End Sub

Sub SetProgressDrawable(p As ProgressBar, drawable 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(r.GetStaticField("android.R$id", "progress"), clipDrawable), _        
            Array As String("java.lang.int", "android.graphics.drawable.Drawable"))
End Sub
 

Jamsa

Member
Licensed User
I can't, sorry :(

I'm not finished integrating it into my app yet ;)

If it helps solving the issue, I can say it's opened from a scrollview, and another scrollview that has a tabhost with a panel where the progressbar is placed. :)

It happens every time I open one item, close it and open a item - it doesn't matter if it's the same or a different item.
 

dbergusa

Member
Licensed User
Same issue

I've noted that the same thing occurs anytime you try to change the color of the progressbar once it has been modified. Basically, trying to call the sub SetProgressDrawable twice on the same object results in a gray bar without any colors. Single calls to the SetProgressDrawable sub work fine, but trying to use it to modify a bar that has already had the new color applied results in failure to display any color at all.

Any thoughts on why?
 

elitistnot

Member
Licensed User
Didn't work for me at all

The method listed to colorize a progressbar didn't work for me. All it did was turn my progressbar grey and makes it look like a disabled progressbar.

Anyway, hope someday to see progressbar.barcolor and progressbar.backgroundcolor so we can easily set the colors with out all the hoops to jump through just to simply change the color. Yellow bars are getting pretty boring.

Thanks.
 
Top