Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to create triangle and rectangle shape programmatically?

How can we create ballon drawable shape as below. where we can change the color of it dynamically. enter image description here

like image 841
Dory Avatar asked Feb 26 '14 13:02

Dory


People also ask

How do you draw a triangle in drawable android?

This example demonstrate about How to draw triangle shape in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.


2 Answers

Here it is XML for triangle and rectangle. save it inside drawable folder.

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: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> 

rectangle.xml

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >     <item>      <shape android:shape="rectangle">       <solid android:color="#B2E3FA" />      </shape>   </item> </layer-list> 

and layout for shape you require.

<RelativeLayout         android:id="@+id/rlv1"         android:layout_width="150dp"         android:layout_height="50dp"         android:background="@drawable/rectangle" />      <RelativeLayout         android:id="@+id/rlv2"         android:layout_width="50dp"         android:layout_height="50dp"           android:layout_below="@+id/rlv1"         android:background="@drawable/triangle"         android:rotation="180" /> 

enter image description here

set margin according you required.

Source

like image 167
Sanket Kachhela Avatar answered Sep 30 '22 23:09

Sanket Kachhela


If you want a border for your layout

enter image description here

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/linear_root"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:orientation="vertical"     >      <TextView         android:id="@+id/text_message"         android:layout_width="100dp"         android:layout_height="wrap_content"         android:background="@drawable/bg_rectangle"         android:layout_marginLeft="20dp"         android:layout_marginRight="20dp"         android:layout_marginTop="20dp"         android:padding="8dp"         android:text="Abc"         />      <ImageView         android:id="@+id/image_arrow"         android:layout_marginTop="-1.5dp"         android:layout_width="16dp"         android:layout_height="16dp"         android:layout_gravity="center_horizontal"         android:background="@drawable/icon_arrow_down"         /> </LinearLayout> 

bg_rectangle

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"     android:shape="rectangle">     <solid android:color="#eaeaea" />     <stroke         android:width="1dp"         android:color="#f00" />     <corners android:radius="8dp" />  </shape> 

icon_arrow_down, or you can create triangle by vector like here

<?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="135%"             android:pivotY="15%"             android:toDegrees="45"             >             <shape android:shape="rectangle">                 <solid android:color="#eaeaea"/>                 <stroke                     android:width="1dp"                     android:color="#f00" />             </shape>         </rotate>     </item> </layer-list> 
like image 45
Linh Avatar answered Sep 30 '22 22:09

Linh