1

I am building an Android Application and I am building a custom progress bar/wheel.

What I want to do is instead of using a plain color to indicate progress, I want to use 2 colors (blue and white for now) but the colors aren't supposed to be gradient, it should just be blue and white next to one another, no gradient whatsoever.

So far, this is what I've tried. I have a gradient_bg.xml file declared as such:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <gradient
        android:type="linear"
        android:startColor="#FFFFFFFF"
        android:endColor="#FF008080"
        android:angle="90"/>
</shape>

and I use it in my code as such:

barPaint.setColor(getResources().getColor(R.drawable.gradient_bg));

However, since it's not a color, I can't use it as a color. If I remove the getResources().getColor() lines, I end up with with a gray-ish color. Also, another problem I'm anticipating is that when the progress/wheel increments in progress, I will no longer have a Blue-White-Blue-White segment, but rather, a Blue-Blue-White-White segment.

halfer
  • 18,701
  • 13
  • 79
  • 158
Razgriz
  • 6,640
  • 12
  • 63
  • 138

1 Answers1

0

you can use a layer-list to compose more complex drawables. Please refer to this StackOverflow question on how to: Understanding Android's <layer-list>

Tthen you can use drawabe to draw it:

  • get the drawable Drawable d = getResources.getDrawable(R.drawable.two_colors);
  • set the size of the drawable to match your custom bar d.setBounds(0, 0, width, height);
  • and draw on the view canvas d.draw(canvas);

I hope it helps

Community
  • 1
  • 1
Budius
  • 37,587
  • 15
  • 92
  • 134