TextView in Android

TextView is the most commonly used for showing pre-defined text on the display screen/View.

This is a view which are not only used alone also used along with other Views as well, like when you are creating a form, where you have EditText view, CheckBox view etc and then labels should be mentioned along with other information and we have to use the TextView alongside.

Attribute Description
android:text It used to specify the text to be displayed in the TextView
android:textSize By using this attribute we can control the size of the text.
android:textColor By ssing this attribute we can specify the color of our text.
android:textAllCaps If it set as True, this will make the text appear in upper case.
android:letterSpacing using this attribute we can set the space between letters of the text.
android:hint The attribute is used for showing a default text, if no text is set in the TextView.Generally whenever we want to populate a TextView using dynamic data which is coming from the server(using the programmatic approach) and we set this attribute to show some default text in the TextView until data is fetched from the server.

Now let us see how to define a TextView in the design layout XML:


First we open the tag with < and type TextView, indicating we are creating a TextView that will get displayed in the app and see what the attributes mentioned above do.

As mentioned earlier, android:layout_width=”wrap_content” and android:layout_height=”wrap_content” which are compulsory to write for every view, because we must define the width and height for every view that we create.

  • android:id=”@+id/tv_text_gender”
    This gives an id(identification) to this textview and which is be used in Java files to access this textview and also to make any changes to it dynamically.
  • android:text=”Salesforcedrillers”
    The TextView attribute will show the given value in the attribute as the text.
  • android:textSize=”45sp”
    This is used for determining the size of the text displayed in the TextView.
  • android:padding=”20dp”
    This provides a padding of 20dp around the text.
  • android:textColor=”#DD2C00″
    This sets the color of the text. #DD2C00 is the Hexa code for color some dark red shade.


Subscribe Now