Making a Triangle Shape Using Xml Definitions

How to draw triangle shape in android xml

Try this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/rightArrow">
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="87%" >
<shape
android:shape="rectangle" >
<stroke android:color="@android:color/transparent" android:width="10dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>

For the desired direction of triangle you can play with the degrees and pivotx/y points

e.g

<rotate
android:fromDegrees="220"
android:toDegrees="0"
android:pivotX="35%"
android:pivotY="5%" >
<shape
android:shape="rectangle" >
<stroke android:color="#000000" android:width="1dp"/>
<solid
android:color="#000000" />
</shape>
</rotate>

will give you 'v' shaped triangle

alternate for rotate
this property also given v shape triangle

android:rotation="180"

Android: Make a button with triangle shape using xml definitions (drawable)

If someone still has issues with this :

  1. xml:

    <item android:top="45dp">
    <shape>
    <size android:height="100dp" android:width="90dp"/>
    <solid android:color="@android:color/holo_orange_light" />
    </shape>
    </item>
    <item android:top="36dp" android:left="11dp">
    <rotate
    android:fromDegrees="45"
    android:toDegrees="0"
    android:pivotX="80%"
    android:pivotY="20%" >
    <shape>
    <size android:width="40dp"
    android:height="30dp"/>
    <stroke android:color="@android:color/holo_orange_light" android:width="1dp"/>
    <solid android:color="@android:color/holo_orange_light" />
    </shape>
    </rotate>
    </item>
    </layer-list>
  2. override TextView and use it in your layout:

    public class CustomTextView extends TextView {

    private int mWidth;
    private int mHeight;


    public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);

    }

    @Override
    protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);
    Paint mPaint = new Paint();
    int color = getResources().getColor(R.color.YourColor);

    mPaint.setColor(color);
    Path mPath = new Path();
    mPath.moveTo(.0f, this.getHeight());
    mPath.lineTo(0.8f * this.getWidth(), this.getHeight());
    mPath.lineTo(this.getWidth(), 0.5f * this.getHeight());
    mPath.lineTo(0.8f * this.getWidth(), .0f);
    mPath.lineTo(.0f, .0f);
    mPath.lineTo(.0f, this.getHeight());

    canvas.clipPath(mPath);
    canvas.drawPath(mPath,mPaint);


    }
    }

Regarding the xml example: there are two rectangles overlapping.You have to play around with the values a lot and this makes it difficult to use on different views. I think using a custom view is the best solution in this case.

Drawing triangle in XML

i can draw triangle shape using XML
triangle .xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<rotate
android:fromDegrees="45"
android:pivotX="-40%"
android:pivotY="87%"
android:toDegrees="45" >
<shape android:shape="rectangle" >
<stroke
android:width="10dp"
android:color="#00000000" />

<solid android:color="#00ACED" />
</shape>
</rotate>
</item>
</layer-list>

triangle

How to create Triangle shaped drawable?

Sample Image drawable-backround_triangle.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="250%"
android:pivotY="80%">
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent" />
</shape>
</rotate>
</item>
</layer-list>

then make your view as below example:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="4dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="false">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_triangle">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="@dimen/dp8"
android:weightSum="1">

<LinearLayout
android:id="@+id/img_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center"
android:orientation="vertical">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/contact_img"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:layout_marginTop="@dimen/dp8"
android:src="@drawable/ic_cars" />
</LinearLayout>

<RelativeLayout
android:id="@+id/content_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dp8"
android:layout_toRightOf="@+id/img_view">

<TextView
android:id="@+id/tv_request_status"
style="@style/txtview_standard_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/dp8"
android:text="Pending"
android:textColor="@color/colorAccent" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_request_status"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp4"
android:layout_marginTop="@dimen/dp4"
android:weightSum="1">

<TextView
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Phone" />

<TextView
android:id="@+id/tv_phone_number"
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:text="Phone" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp4"
android:layout_marginTop="@dimen/dp4"
android:weightSum="1">

<TextView
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Email" />

<TextView
android:id="@+id/tv_email"
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:text="joe@gmail.com" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp4"
android:layout_marginTop="@dimen/dp4"
android:weightSum="1">

<TextView
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Age" />

<TextView
android:id="@+id/tv_age"
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:text="Phone" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="@dimen/dp4"
android:weightSum="1">

<TextView
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Gender" />

<TextView
android:id="@+id/tv_gender"
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:text="Phone" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp4"
android:layout_marginTop="@dimen/dp4"
android:weightSum="1">

<TextView
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Efficiency" />

<TextView
android:id="@+id/tv_efficiency"
style="@style/txtview_standard_widget"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:text="75%" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>

</android.support.v7.widget.CardView>

android:How to make triangular layout

You can create this custom shape using <layer-list>. Below is a working example. Put custom_triangular_shape.xml into your res/drawable folder.

custom_triangular_shape.xml

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

<!-- Transparent Rectangle -->
<item>
<shape android:shape="rectangle">
<size
android:width="300dp"
android:height="80dp" />
<solid android:color="@android:color/transparent" />
</shape>
</item>

<!-- Colored Rectangle -->
<item
android:bottom="20dp">
<shape android:shape="rectangle">
<corners
android:radius="4dp">

</corners>
<size
android:width="300dp"
android:height="80dp" />
<solid android:color="#FFFFFF" />
</shape>
</item>

<!-- Bottom Triangle -->
<item
android:left="90dp"
android:right="110dp"
android:top="0dp"
android:bottom="30dp">
<rotate android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</rotate>
</item>

</layer-list>

USE:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_triangular_shape">

</LinearLayout>

OUTPUT:

Sample Image

Hope this will help~



Related Topics



Leave a reply



Submit