Search code examples
androidviewline

View Stroke not working in Android above lollipop


Have implemented the View to draw the line its working below kitkat version and it's not working in lollipop and Marshmallow devices.

Below is my code:

 <View
        android:id="@+id/vDottedLine"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="@dimen/sixteen_dp_margin"
        android:layout_marginRight="@dimen/sixteen_dp_margin"
        android:background="@drawable/dotted"
        android:layerType="software" />

XML file for color

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
    android:height="2dp"
    android:color="#000"
    android:dashGap="3dp"
    android:dashWidth="8dp" />

</shape>

And also tried adding android:layerType="software" and android:hardwareAccelerated="true" and false as well. Any hint to overcome this problem?


Solution

  • Try this

    create drawable file :

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="2dp"
        android:color="#999999"
        android:dashGap="5dp"
        android:dashWidth="2dp" />
    <solid android:color="@android:color/transparent" />
    
    <size
        android:width="2dp"
        android:height="2dp" />
    </shape>
    

    Your View :

    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/dash"
        android:layerType="software"
        android:layout_centerInParent="true" />