Add rounded corners border to TextView using xml Android

Creating textView with rounded border using external layout xml file.

Setting up rounded border around textView makes it more easy to understand because after using rounded border it will make textView separate from other layout widgets. Now i am creating another layout xml file with rounded border and set that particular xml file to TextView so it will automatically adjust and set as proper background of TextView. So here is the complete step by step tutorial for Add rounded corners border to TextView using xml Android.

android-project-download-code-button

How to Add rounded corners border to TextView using xml Android.

Code for MainActivity.java file.

package com.textviewroundedborder_android_examples.com;
import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 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:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.textviewroundedborder_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="This is sample textView text including rounded border"
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center"
 android:background="@layout/rounded_border_textview"
 android:padding="10dp"
 />

</RelativeLayout>

 

Code for rounded_border_textview.xml file.

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
 <shape android:shape="rectangle">
 <solid android:color="#ffffff"/>
 <corners android:radius="20dp" />
 <stroke
 android:width="2dp"
 android:color="#3bbdfa"
 />
 </shape>
</item>
</selector>

Screenshot:

Add rounded corners border to TextView using xml Android

Click Here To Download Add rounded corners border to TextView using xml Android project.

Leave a Reply

Your email address will not be published. Required fields are marked *