Madhav_nimavat
Madhav_nimavat

Reputation: 401

How to add more than 1 shape in single xml drawable

I want to create chat bubble using xml resource but i'm not able to make triangle outside of rectangle with right edge i also tried with 9 patch image but when user enters 1 character still it width is not as wrap content. please help me i also put created code of xml resouce.

<item>
    <shape android:shape="rectangle">
        <solid android:color="#5EB888"/>
        <corners android:radius="30dp"/>
    </shape>
</item>


<item
    android:bottom="-1000dp"
    android:right="200dp"
    android:gravity="right"
    android:top="190dp">

    <rotate
        android:fromDegrees="-45">
        <shape android:shape="rectangle">
            <solid android:color="#000000"/>
        </shape>
    </rotate>
</item>

enter image description here

Upvotes: 1

Views: 1299

Answers (2)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Here is the working drawable custom_shape_chat_box.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="60dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <!-- Colored Rectangle -->
    <item
        android:right="20dp">
        <shape android:shape="rectangle">
            <size
                android:width="300dp"
                android:height="60dp" />
            <solid android:color="#5EB888" />
        </shape>
    </item>

    <!-- Bottom-Right Triangle -->
    <item
        android:left="20dp"
        android:right="0dp"
        android:top="-10dp"
        android:bottom="20dp">
        <rotate android:fromDegrees="26">
            <shape android:shape="rectangle">
                <solid android:color="#5EB888" />
            </shape>
        </rotate>
    </item>

</layer-list>

USE:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="24dp">

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

OUTPUT:

enter image description here

Hope this will help~

Upvotes: 0

Jaymin
Jaymin

Reputation: 2912

Check out these libraries

It may help you (Y)

Bubble 1

Bubble 2

Bubble 3

Upvotes: 1

Related Questions