REQUIREMENT
How do I create a view looking like this.
I would like to draw a view on the screen which is a line broken into segments showing values percentage of the whole view. My requirements are
- view has different sections which are different colors
- the view might not have all sections rendered, it might only have first 2 or first and last or just a single color etc - this is only known at runtime
- the size of the different sections are only known at runtime therefore need to be specified programmatically
- left and right corners of the whole view are rounded
IDEAS/THINGS I HAVE TRIED
(1) Custom view rendering 3 rectangles side by side
I have tried a custom view which renders 3 rectangles side by side. But these obviously have square corners.
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int viewHeight = 50;
canvas.drawrect(0, 0, 60, viewHeight, paint); // A
canvas.drawrect(60, 0, 120, viewHeight, paint); // B
canvas.drawrect(120,0, 180, viewHeight, paint); // C
}
(2) Shape with rounded corners
I know I can use a Shape to define a rectangle with rounded corners using the following, but this is a single color.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
...
<corners
android:radius="4dp" />
....
</shape>
(3) Layer-list
From Android rectangle shape with two different color, I see I can use a layer-list to specify each item in the shape to have different colors.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<size
android:width="40dp"
android:height="40dp" />
<solid android:color="#F86F05" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle">
<size
android:width="30dp"
android:height="30dp" />
<solid android:color="#B31F19" />
</shape>
</item>
</layer-list>
(4) Layer-list with corners??
Can I add the "corners" tag to the whole layer-list to get the rounded main corners? I assume not, and that the corners part has to be in the "Item"s shape tags.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:radius="4dp" />
<item>
<shape android:shape="rectangle">
<size
android:width="40dp"
android:height="40dp" />
<solid android:color="#F86F05" />
</shape>
</item>
<item android:top="10dp">
<shape android:shape="rectangle">
<size
android:width="30dp"
android:height="30dp" />
<solid android:color="#B31F19" />
</shape>
</item>
</layer-list>
SUMMARY
This last one is getting closer to my requirement however
- how do i specify each "item"s width programmatically
- how do i show/hide "item"s programmatically
- how would i round just the top most visible "item"s top corners and the bottom most "item"s bottom corners
UPDATE: HOW DO I ADD ELEVATION/GREY BORDER
Thank you to "@0X0nosugar" for your solution. I am now wanting to add an elevation or a slight grey border as one of the colors is faily light and close to the background color. When I add the following I get a rectangular shadow which looks terrible with the curved corners.
android:elevation="2dp"
android:outlineProvider="bounds"
I would like it to appear like below