Dotted dash vertical line with gradient color


I want to make dotted line vertically with the gradient color effect. But I'm unable to do that. How can I approach. I am using this code on drawable for the dotted line

  <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:left="3px"
    android:right="-5px"
    android:top="-5px"
    android:bottom="-5px">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">

        <stroke
            android:width="4px"
            android:color="@color/colorPrimary"
            android:dashGap="20px"
            android:dashWidth="20px" />
    </shape>
</item>

</layer-list>

Anyone knows please help Thanks in advance


Answers:


create a drawable like below

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

        android:bottom="-5px"
        android:left="3px"
        android:right="-5px"
        android:top="-5px">
        <shape>

            <gradient
                android:angle="270"
                android:centerColor="#00ff00"
                android:endColor="#ff0000"
                android:startColor="#0000ff"/>

            <stroke
                android:width="2dp"
                android:color="#fff"
                android:dashWidth="8dp"
                android:dashGap="16dp"/>
        </shape>
    </item>
</layer-list>

now in layout you need to use a view and set this shape as background to it.

<View
    android:layout_width="4dp"
    android:layout_height="match_parent"
    android:background="@drawable/your_shape"/>

you might need to change widht settings as per your requirements. Here is what I got

enter image description here