Android Tutorial Change color of Android ProgressBar

texwillerx

Member
Licensed User
Progressbar shows only a thin blue image

When I use the following code, the progressbar shows only a very thin blue image (of course moving).

How can I make this image to have a height of the progressbar?

Thanks and regards

progressbar1.Initialize("")
prinfopanel.AddView(progressbar1,0,0,0,0)

progressbar1.left=20dip
progressbar1.top=100dip
progressbar1.width=100%x-20dip
progressbar1.height=50dip
 

texwillerx

Member
Licensed User
For other applications, when I use progressbar, I get a Basic4android style progress bar whose height is what I give.

Hence, what should I do in order to get a basic4android progress bar?

Thanks
 

GMan

Well-Known Member
Licensed User
I read out several datas, also the ec-values of the humidity of earth (from plants) and disliked the standard color of the progrssbars which gives the percentage of humidity.
With this lib i made them in cool blue shades and now it looks like water - so very good AND useful AND nice looking lib :)
 

Croïd

Active Member
Licensed User
I'm not understand, this is impossible to achieve a basic progressbar with Timer ?

B4X:
Sub Process_Globals
Dim timer1 As Timer
End Sub

Sub Globals
   Dim pb As ProgressBar
   Dim gd As GradientDrawable
End Sub
Sub Activity_Create(FirstTime As Boolean)

    pb.Initialize("pb")
    gd.Initialize("TOP_BOTTOM", Array As Int(Colors.Green, Colors.LightGray))
    gd.CornerRadius = 30dip
    SetProgressDrawable(pb, gd)
    Activity.AddView(pb, 10dip, 10dip, 300dip, 10dip)
 
timer1.Initialize("timer1",1000)
timer1.Enabled = True

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

Sub timer1_tick
pb.Progress = 1 + 5
If  pb.Progress > 100 Then
ToastMessageShow("Hello",True)
timer1.Enabled = False
End If
End Sub
 

Troberg

Well-Known Member
Licensed User
Wasn't easy but here it is:(snip)
Nice, and even better, it also works on seekbars with a tiny change:

B4X:
Sub SetSeekDrawable(sb As SeekBar, 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 = sb
   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
Only change is that the first argument is redefined as a seekbar, instead of a progressbar. From there on, i's the same (although I renamed it for consistency).
 
Last edited:

Marcob

Member
Licensed User
Not with this code. You may be able to do it with a custom XML layout file.
To whom it may concern here is an example:

1) Create a file pgb.xml in drawable dir:

B4X:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
  <shape>
    <corners android:radius="5dip" />
    <gradient
      android:startColor="#ff9d9e9d"
      android:centerColor="#ff5a5d5a"
      android:centerY="0.75"
      android:endColor="#ff747674"
      android:angle="270" />
  </shape>
</item>

<item android:id="@android:id/secondaryProgress">
  <clip>
    <shape>
      <corners android:radius="5dip" />
      <gradient
        android:startColor="#80ffd300"
        android:centerColor="#80ffb600"
        android:centerY="0.75"
        android:endColor="#a0ffcb00"
        android:angle="270" />
    </shape>
  </clip>
</item>

<item android:id="@android:id/progress">
  <clip>
    <shape>
      <corners
        android:radius="5dip" />
        <gradient
          android:startColor="#A00000"
          android:endColor="#400000"
          android:angle="270" />
    </shape>
  </clip>
</item>

</layer-list>
2) Add in ProgressBar xml file (layout dir) the following line:

B4X:
android:progressDrawable="@drawable/pgb"
3) In the Project Attributes region add the line:

B4X:
#CustomBuildAction: 1, c:\windows\system32\attrib.exe, +r res\*.* /s
 
Top