Search code examples
androidandroid-drawableandroid-shape

round the two ends of thick line with xml shape


I have a line shape, it is a thick line:

<?xml version="1.0" encoding="utf-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="30">
    <shape android:shape="line">
        <size
            android:width="100dp"
            android:height="100dp" />
        <stroke
            android:width="30dp"
            android:color="#0000FF" />
    </shape>
</rotate>

I am wondering is there any way to make the line's two ends rounded?

I tried adding this to above <shape>:

<corners android:radius="10dp"/>

But it doesn't take any effect for thick line.


Solution

  • Add a View tag in your xml something like this which represents the line. Adjust the height for varying thickness of the line.

    <View
       android:layout_width="100dp"
       android:layout_height="10dp"
       android:background="@drawable/test"/>
    

    And then add the usual background for it like you've done

    test.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#000000" />
    <corners android:radius="8dip" />
    </shape>
    

    This first gave me no start tag found error but on clean and rebuild it produced the required output. Try it. Hope this helps.