Search code examples
androidandroid-drawableshapesandroid-shape

how to draw a trapezium for android shape?


edited part of the question I want to draw a trapezium shape to apply for a Linear-layout. so far I've done following. But I want to only to appear the trapezium. other parts must be not visible. thank you. i dont want the othe parts except trapezuim


Solution

  • Here you go:

    Create an layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:background="@drawable/myshape"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.williamkinaan.expresso.Main2Activity">
    
    </RelativeLayout>
    

    Notice that the layout has a drawable background which is: myshape.xml

    myshape.xml

    Create myshape.xml file in the drawbable folder:

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="100dp" android:height="100dp" android:viewportHeight="100" android:viewportWidth="100">
        <group>
            <path android:pathData="M0 0 L100 0 L100 50 L75 100 L 25 100 L 0 50 Z"
                android:strokeColor="#7e0b0b"
                android:strokeWidth="2"/>
            <path android:pathData="M100 0 L50 80 L0 0" android:strokeColor="#7e0b0b"
                android:strokeWidth="2"/>
            <path android:strokeColor="#7e0b0b" android:strokeWidth="2" android:pathData="M80 30 L20 30"/>
            <path android:strokeColor="#7e0b0b" android:strokeWidth="2" android:pathData="M50 80 L63 100" />
        </group>
    </vector>
    

    Result

    enter image description here

    After your edit

    Change myshape.xml to:

    <?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="100dp"
        android:height="100dp"
        android:viewportHeight="100"
        android:viewportWidth="100">
        <path
            android:pathData="M0 0 L100 0 L75 40 L25 40 Z"
            android:strokeColor="#7e0b0b"
            android:strokeWidth="2" />
    
    </vector>
    

    The result

    enter image description here