How to add border on Android TextView

In Android TextView is a widget that allows you to display text. In this Android tip, I am going to show you how to add a border to the TextView in layout xml file and in code.

To achieve this goal, first you need to create a drawable resource xml file called border_style.xml. This file will be used to apply the border style to the TextView. The <stroke> tag specifies the width and color of the border. To make the border of the TextView more or less rounded, you will increase or decrease the value of the attribute android:radius in the <corners> tag. In the <padding> you can specify the spaces between the text and the border.


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android=" http://schemas.android.com/apk/ res/android" android:thickness="0dp" android:shape="rectangle">

<stroke android:width="2dp"
       android:color="#44aa77"/>

<corners android:radius="10dp"    
     />

<padding android:left="10dp"
       android:top="5dp"   
       android:right="10dp"
       android:bottom="5dp"/>

</shape>

Then open the activity_main.xml file to add a TextView as shown below. You will add the android:background attribute to TextView to use the border_style.xml file.

<LinearLayout 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:orientation="vertical"
   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=".MainActivity" >

   <TextView
     android:id="@+id/text_view"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:textSize="20sp"
     android:background=" @drawable/border_style"
      />

</LinearLayout>

In the onStart method of the MainActivity class, simply write code to display sample text in the TextView as shown below.

public void onStart(){
   super.onStart();
   TextView tv= (TextView)findViewById(R.id.text_view);
   tv.setText("This TextView has a border.\r\n. The border color code is 44aa77.");
  
   }



The description above shows you how to add a border to the TextView in xml file. If you want to add the border to the TextView in code, you will remove the android:background attribute of the TextView from the activity_main.xml file. Then in the onStart method, add the following line immediately below or above the line that you call the setText method:

tv.setBackgroundResource( R.drawable.border_style);

How-to-add-border-on-Android-TextView

Posted by: Dar | post date: 06-29-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.