Create a TextView in Android

Generic placeholder image
Satya
android  

Date:-October 01st, 2018 , Posted On:- Mobile Development


In this blog post, we learn how to create a text view in android.

TextView in Android

TextView is mostly used to show pre-define text on the Android display. this view is not used alone, but this is also used along with another view. suppose when you create a form where you have Edit Text etc. where you add labels and other information.

Following are some important of the main attributes that are most commonly used :

Attribute Description
android:text Used to specify the text to be displayed in the TextView area.
android:textSize Using this attribute to control the size of the text.
android:textColor Using this attribute to define the color of our text.
android:textAllCaps If set True, this attribute will make the text appear in upper case.
android:letterSpacing Using this attribute we can set the spacing between letters of the text.
android:hint This is used to show a default text if no text is set in the TextView. Generally, when we populate a TextView using dynamic data coming from the server(using the programmatic approach), then we set this attribute to show some default text in the TextView until data is fetched from the server.

Now lets we going to define a TextView in the design layout with XML.

<TextView
    android:id="@+id/st"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Studytonight"
    android:textSize="45sp"
    android:padding="20dp"
    android:textColor="#DD2C00"/>

 

First, we start the tag with  < TextView, indicating we are creating a TextView that will get displayed in the app. Now let's see what the attributes mentioned above do.

android:id="@+id/tv_text_gender"

This tag gives an id(identification) to this text-view which can be used in Java files to access this text view and to make changes to it dynamically. For example, we have some text as "Code". But we want to change as "Codermen" after we touch the screen. So, we use this in our id Java class and we can use some predefined function to change the text of the Text view during runtime when the user taps on it.

android:text="CoderMen"

Using this Text view show how the given value in the attribute as the text.

android:textSize="50sp"

This is used to set the font size of the text displayed in the TextView.

android:padding="25dp"

This is used to provides a padding of 25dp around the text. 

android:textColor="#DD2C05"

This is used to sets the color of the text. #DD2C05 is the hexa code for color some dark and red shade.

In Android Studio some predefined colors in color.xml file.

So here we completed Create a TextView in Android blog post.


About the author

0 comments

You must be logged in to post a comment.