TextView in Android Studio with Programming Examples

Introduction of TextView in android:

As we know, TextView is a fundamental and vital UI element in an Android. As the name suggests, it plays a very crucial part in the UI experience and assists as to how information has to be presented to a user. Its widget being found on an Android can effectively be used in multiple situations. For instance, if we need some important information to be highlighted, then its substring has to be italicized or it has to be turned bold.  Another situation, where TextView can be benefitted from, is that if the information has a hyperlink, which guides us to a particular web URL, then it has to be bridged with hyperlink and properly underlined.

TextView in Android exhibits text to a user and offers him the option to edit it programmatically. TextView is a complete text editor. Although it is basically configured as “editing not allowed” however, luckily, we can make use of this useful widget in order to edit and make changes to a text as desired.

A view can be treated as a parent class of TextView whereas a Text View as a subclass of view. As such,  text view can be used as a component in app’s GUI inside a ViewGroup, or it may be deemed as a content view of the action.

A TextView case can be formed via pronouncing it inside an arrangement or design (XML file) or through exemplifying it programmatically (Java Class).

Attributes of Textview in android:

In order to configure a TextView in an XML file, we discuss the attributes (qualities or features) by means of which this task can better be comprehended.


it is a feature or peculiar characteristic which is used to distinctively recognize a textview. An example code has been represented below, in which the id of a text view has to be set.


it is a non-compulsory attribute that is used in order to regulate the text orientation as left, right, center, top, bottom, center-vertical, center-horizontal, etc.

An example code with elucidation has been given below, where we have to set the right and left gravity for the text of a TextView.



this attribute is used to manage some sort of text in a text view.  The text can be set in XML as well as in the java class.

An example code with explanation has been given below in which we set the text “Programming Digest” in a text view.

In Java class:

An example code has been given below, in order to set the text in a textview programmatically, i.e. in the java class.



 In order to set the color of a text in text view, this attribute is commonly used. In android for color, the RGB form is used.

An example code with explanation has been illustrated below, in which green color has been chosen in order to exhibit the proposed text.

In Java class:

To set the color of the textview widget with java programming the following code example is used to set the color



We can make use of TextSize attribute in order to set size of the text of a text view.  The text size can be set either in sp (scale independent pixel) or dp (density pixel).

An example code has been mentioned below in order to set the 50dp size for the text of a text view.

In Java class:

An example code has been given below, in which we set the text size of a text view programmatically means in java class.



 In order to set the style of a text view, we make use of textStyle attribute.

In android there are only bold, italic, and normal text styles are available. If we use more than two styles then this “|” operator is used.

An example code with explanation has been given below in which we set the bold and italic text styles for text.


Applying More than two textstyle



in order to set background of a text, this attribute has to be used. The color or drawable can be set in the background of a text by making use of this background feature.


In Java class:

An example code in which we set the background color of a text view programmatically means in java class, has been given below.


In order to set the padding from left, right, top or bottom, this attribute is used. In afore – mentioned background example code, we also set the 10dp padding from all the sides of text view.

An example code with explanation has been given below is the example, in which a black color is set for the background, white color for the text to be displayed and a 40dp padding set from all the side’s for text view.



Android offers mainly 3 types of typefaces




The above three types of faces are to be projected under the “typeFace” attribute of the TextView in XML.

Use the following code and see the  “typeFace” attribute of the TextView.


Text Shadow:

We can also set the shadow to the textview in android. The attributes used for shadows in android are given below


This attribute is used to set the distance of the shadow in x axis and its value must be integer


This attribute is used to set the distance of the shadow in y axis          and its value must be integer


This attribute is used to set the radius of the textview shadow

Copy the below code and paste it and checks its output


Letter Spacing:

  • An important property of the text view in android is letter spacing and capital letters.
  • To set the buttons and tab layouts text, google material design recommended it to use uppercase letters
  • The letter spacing also should be sustained according to the situation.


To giving the spaces between each character this attribute is used. And its value must be floating type.

Copy the below code and paste it and checks its output


All Caps:

android:textAllCaps: To change all the characters in uppercase or in lowercase this attribute is used. And its value must be true or false.

Copy the below code and paste it and checks its output


Example1: how to change Text of the TextView with button:

An example of TextView in which we display a text view and set the text in xml file and then change the text on button click event programmatically, has been given below. The final output and code are as follows:

Step 1: Create a new project and name it TextViewDemo.

Step 2: Open the res folder then click on activity_main.xml and add the following code. Here I created a button and a textview in Relative Layout.

Step 3: than open and add the following code. In this code I am changing the text of the textview when the user clicked on the button


Now run the app in Emulator or in a mobile phone, and click on the button. You will see text will be change when you clicked on the button.


When you clicked on the button the text of the textview will be changed, as you can see in the below figure


Example2: how use different Android TextView Attributes:

The usage of TextView in an android application, has been elaborated as follows.

First, create new android studio project and choose the name in my case I set application name as TextViewDemo. If you don’t know how to create and android studio application then click this article Toast Message in Android Studio.

Then click on res folder in res folder click on layout in layout folder open activity_main.xml file and paste the below code.

Once the layout has been created with required controls, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file and paste the below code.

If you observe above code I am calling the  layout using setContentView method in the form of R.layout.layout_file_name(R is class, layout is a method and then file name. I my case my xml file name is activity_main.xml so we used file name activity_main and I am setting text to one of my TextView control (TxtDemo) in our activity file.

Output of Android TextView Example

Now click on the run button you will see the output like as shown in below


Example3: how to Add Icons for TextView

  • In android we can also use icons with textview
  • In android There are three positions to add the icons with TextView. The positions are start, end, top, and bottom.

To create drawable icon for textview using asset studio  simple right click on the drawable folder, then click on new in new dropdown list click on Vector asset


In asset studio click on clip Art for selecting the icon


Then select icon as per your required in my case I selected the back arrow icon, and then click on the ok button


Then set the icon name, and click on the next button


Finally, click on the finish button


As you can see the back.xml file is created


Repeat the same steps for the forward button


Copy the following code and check its output, to know how to add the drawable icons with Text View.


How to Add custom fonts in android

Any custom font can actually be used which we desire to have in our android applications. For this purpose, you can download any fonts from 1001freefonts website. For example, I downloaded Motion Picture font as an example.

Once your TTF font file is downloaded place it into asset/font folder



Here I am using a very basic layout. In this layout I use one textview with an id of TxtDemo.

In order to set the custom font manually, we have to open activity file and insert this into the onCreate() method:


Related Articles

Leave a Reply

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

Back to top button