TextView is the most widely used view used to show pre-defined text on display screen.
This is a view which is not only used alone, but is also used along with other Views as well, like when you are creating a form, where you have
CheckBox view etc, then to mention the labels and other information, we will have to use the
Following are some of the main attributes that are most commonly used:
|Used to specify the text to be displayed in the TextView|
|Using this attribute we can control the size of the text.|
|Using this attribute we can specify the color of our text.|
|If set True, this will make the text appear in upper case.|
|Using this attribute we can set the spacing between letters of the text.|
|This attribute 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 server(using the programmatic approach), then we set this attribute to show some default text in the TextView until data is fetched from server.|
Now lets see how to define a TextView in the design layout 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 open the tag with
< and type TextView, indicating we are creating a TextView that will get displayed in the app. Now lets see what the attributes mentioned above do.
As mentioned earlier, android:layout_width="wrap_content" and android:layout_height="wrap_content" are compulsory to write for every view, because we must define the width and height for every view that we create.
This gives an
id(identification) to this textview which can be used in Java files to access this textview and to make any changes to it dynamically. For example, suppose we set some initial text as "study". But we want the text to be as "studytonight" after we touch the screen. So, we use this
id in our Java class and use some predefined function to change the text of the TextView during runtime, when the user taps on it. We will learn how to do so very soon.
The TextView will show the given value in the attribute as the text.
This is used to determine the size of the text displayed in the TextView.
This provides a padding of 20dp arround the text. More on padding here in this tutorial.
This sets the color of the text.
#DD2C00 is the hexa code for color some dark red shade. You can easily look for Hexa codes for various colors from ColorHexa or from the official material design website - Material Colors
Also, there are some predefined colors in Android Studio that are stored under color.xml file in the values directory. To access this, you can use
"@android:color/your_color_name" as the value in the above attribute.
You can also define the colors which you use oftenly in your app in the color.xml styles file. It is a very good practice to do so, because if you decide to change the color code/shade, all you have to do is change it in the color.xml file and it will get changed everywhere.