- Android TextView is simply a view that are used to display the text to the user and optionally allow us to modify or edit it. First of all, open Kotlin project in Android Studio.
1. Formatting the TextView
- Android offers mainly 3 types of typefaces:
- normal , sans , serif , monospace
- The above four types of faces are to be invoked under the “typeFace” attribute of the TextView in XML
3. Changing Text Style
- In Android there are basically three text styles:
- Bold , Italic , Normal
- The text style of the text in android can be implemented using the attribute “textStyle”.
- Multiple text styles can also be implemented using the pipeline operator. Example “android:textStyle=”bold|italic”.
4. Changing the Text Color
- The color of the text should also change according to the change in the context of the information displayed to the user.
- For example, if there is warning text it must be in the red color and for disabled text, the opacity or the text color should be grayish. To change the color of the text, the attribute “textColor” is used.
- Android also offers the predefined text colors, which can be implemented using “@android:color/your Color” as value for the “textColor”. Here the value may be hex code or the predefined colors offered by the android.
5. Text Shadow
- Shadow for the text can also be given in Android. The attributes required for the shadowed text view are:
- android:shadowDx=”integer_value” -> which decides the distance of text from its shadow with respect to x axis, if the integer_value is positive the shadow is on positive of the x axis and vice versa.
- android:shadowDy=”integer_value” -> which decides the distance of text from its shadow with respect to y axis, if the integer_value is positive the shadow is on negative of the y axis and vice versa.
- android:shadowRadius=”integer_value” -> which decides the amount of the shadow to be given for the text view.
6. Letter Spacing and All Caps
- Letter spacing and capital letters are some of the important properties of the text View in android.
- For the text of buttons and tab layouts, the text should be in uppercase letters recommended by Google Material Design.
- The letter spacing also should be maintained according to the scenario.
- android:letterSpacing=”floatingTypeValue” -> This attribute is used to give the space between each of the letters.
- android:textAllCaps=”trueOrfalse” -> This attribute decides, all the letters should be in uppercase or not.
7. Adding Icons for TextView
- Android also allows adding drawable with the text views.
- There are three positions to add the icons for the TextView.
- They are a start, end, top, and bottom.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" tools:ignore="HardcodedText"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="64dp" android:drawableStart="@drawable/ic_lappy" android:padding="4dp" android:text="GeeksforGeeks" android:textColor="@android:color/black" android:textSize="32sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="64dp" android:drawableEnd="@drawable/ic_lappy" android:padding="4dp" android:text="GeeksforGeeks" android:textColor="@android:color/black" android:textSize="32sp" /> </LinearLayout>
Output:
Different attributes of TextView widgets –
Attributes | Description |
android:text | Sets text of the Textview |
android:id | Gives a unique ID to the Textview |
android:cursorVisible | Use this attribute to make cursor visible or invisible. Default value is visible. |
android:drawableBottom | Sets images or other graphic assets to below of the Textview. |
android:drawableEnd | Sets images or other graphic assets to end of Textview. |
android:drawableLeft | Sets images or other graphic assets to left of Textview. |
android:drawablePadding | Sets padding to the drawable(images or other graphic assets) in the Textview. |
android:autoLink | This attribute is used to automatically detect url or emails and show it as clickable link. |
android:autoText | Automatically correct spelling errors in text of the Textview. |
android:capitalize | It automatically capitalize whatever the user types in the Textview. |
android:drawableRight | Sets drawables to right of text in the Textview. |
android:drawableStart | Sets drawables to start of text in the Textview. |
android:drawableTop | Sets drawables to top of text in the Textview. |
android:ellipsize | Use this attribute when you want text to be ellipsized if it is longer than the Textview width. |
android:ems | Sets width of the Textview in ems. |
android:gravity | We can align text of the Textview vertically or horizontally or both. |
android:height | Use to set height of the Textview. |
android:hint | Use to show hint when there is no text. |
android:inputType | Use to set input type of the Textview. It can be Number, Password, Phone etc. |
android:lines | Use to set height of the Textview by number of lines. |
android:maxHeight | Sets maximum height of the Textview. |
android:minHeight | Sets minimum height of the Textview. |
android:maxLength | Sets maximum character length of the Textview. |
android:maxLines | Sets maximum lines Textview can have. |
android:minLines | Sets minimum lines Textview can have. |
android:maxWidth | Sets maximum width Textview can have. |
android:minWidth | Sets minimum lines Textview can have. |
android:textAllCaps | Show all texts of the Textview in capital letters. |
android:textColor | Sets color of the text. |
android:textSize | Sets font size of the text. |
android:textStyle | Sets style of the text. For example, bold, italic, bolditalic. |
android:typeface | Sets typeface or font of the text. For example, normal, sans, serif etc |
android:width | Sets width of the TextView. |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!-- Used to Display Text --> <TextView android:id="@+id/text_view_id" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/text_view" android:textColor="#008000" android:textSize="40dp" android:textStyle="bold"/> </LinearLayout>