Android TextView


Download using android TextView sample

Displays text to the user and optionally can allow them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing. TextViews are one of the basis of Android UI framework. Buttons, EditTexts, RadioButtons are subclass of TextView, that means most of the properties that are applicable to TextView will also be applicapable to these views.

Android TextView Usage


An TextView can be added to a layout with all default behaviors with the following XML:

How to retrieve & set value in a TextView?


Retrieving & setting value in a TextView is as follows:

Text decoration in android


Android provides several text decorating attributes like Typeface, font family, text style, text size, text color, text shadow etc. which you can learn here:

How to decorate text in android

Autolinking url


TextView can be autolink an url within its content and can be use to open url in browser.

Result

Autolinking url

How to display image within a TextView


We can use TextView to display image as part of its content area. This is also applicable in EditText, Button, RadioButton, CheckBox as these classes are subclass of TextView.

Here are the drawable attribute that can be use to display image as part of content:

  1. android:drawableStart : The drawable to be drawn to the start of the text.
  2. android:drawableEnd : The drawable to be drawn to the end of the text.
  3. android:drawableLeft : The drawable to be drawn to the left of the text.
  4. android:drawableRight : The drawable to be drawn to the right of the text.
  5. android:drawablePadding : The padding between the drawables and the text.

Result

Image in TextView

How to style section of text in a TextView?


We can use TextView to display image as part of its content area. This is also applicable in EditText, Button, RadioButton, CheckBox as these classes are subclass of TextView.

Result

Style sectionof text

How to create clickable styled span


In certain cases, we might want different substrings in a TextView to different styles and then clickable to trigger an action.

Result

Clickable span

How to avoid text wrapping in TextView?


If a String is longer than the TextView's width it automatically wraps onto the next line. We can avoid this default behaviour by using 'ellipsize' property of a text view.

Result

Data truncation

How to add html formatting in TextView?


If a String is longer than the TextView's width it automatically wraps onto the next line. We can avoid this default behaviour by using 'ellipsize' property of a text view.

Result

Html formatting

Example of android TextView

Result

Android text view

Step 1: Create a new project in Android Studio and name it Android TextView.

Step 2: Open res -> layout -> activity_main.xml and add following code

activity_main.xml

Step 3: Open res -> values -> strings.xml and replace it with below given code.

strings.xml

Step 4: Either download icons that are used in the application from google or replace its name with some system icons.

Step 5: Open your MainActivity.java and replace the source of your MainActivity.java with Source of my MainActivity.java:

MainActivity.java

Step 6: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.