Wednesday, February 1, 2017

Creating Dash line using XML

Tags

Create xml in drawable folder named dash_line.xml and then in the layout just define a view with background as dash_line.


1.dash_horizontal_line.xml

Create an xml in drawable folder named dash_horizontal_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:color="#263238"
        android:dashWidth="2dp"
        android:dashGap="3dp"
        android:width="1dp"/>
</shape>

2.dash_virtical_line.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="-300dp"
        android:right="-300dp">
        <rotate
            android:drawable="@drawable/dash_horizontal_line"
            android:fromDegrees="90"
            android:toDegrees="90"
            android:visible="true"/>
    </item>
</layer-list>

3.activity_main.xml

Note to include android:layerType="software" or include android:hardwareAccelerated="false" to activity in AndroidManifest.xml, otherwise it won't work.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="50dp">
    <View
        android:layout_width="wrap_content"
        android:layout_height="2dp"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="28dp"
        android:background="@drawable/dash_horizontal_line"
        android:layerType="software" />
</LinearLayout>


EmoticonEmoticon