radius rectangle with dynamic size

137 Views Asked by At

enter image description here How do I create a rectangle with a text object inside, so that the width of the rectangle elastically reshapes to the width of the text (rahter than the regular text-wrap in shape behaviour)?

1

There are 1 best solutions below

1
kartik On

Add a border to Android TextView we need to create an XML containing shape as a rectangle file under the drawable's folder and set it as background to the TextView. tag is used to set the border width and color.

textview_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
 <stroke
   android:width="2dp"
   android:color="#000000" />
 </shape>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:padding="10dp" xmlns:tools="http://schemas.android.com/tools" 
      tools:ignore="HardcodedText">

<TextView
      android:id="@+id/textView2"
      android:layout_width="match_parent"
      android:layout_height="30dp"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:background="@drawable/textview_border"
      android:gravity="center"
</RelativeLayout>

Result enter image description here