Android TextView Control


A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.

TextView Attributes

Following are the important attributes related to TextView control. You can check Android official documentation for complete list of attributes and related methods which you can use to change these attributes are run time.

android:idThis is the ID which uniquely identifies the control.
android:capitalizeIf set, specifies that this TextView has a textual input method and should automatically capitalize what the user types.
  • Don't automatically capitalize anything - 0

  • Capitalize the first word of each sentence - 1

  • Capitalize the first letter of every word - 2

  • Capitalize every character - 3

android:cursorVisibleMakes the cursor visible (the default) or invisible. Default is false.
android:editableIf set to true, specifies that this TextView has an input method.
android:fontFamilyFont family (named by string) for the text.
android:gravitySpecifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view.
android:hintHint text to display when the text is empty.
android:inputTypeThe type of data being placed in a text field. Phone, Date, Time, Number, Password etc.
android:maxHeightMakes the TextView be at most this many pixels tall.
android:maxWidthMakes the TextView be at most this many pixels wide.
android:minHeightMakes the TextView be at least this many pixels tall.
android:minWidthMakes the TextView be at least this many pixels wide.
android:passwordWhether the characters of the field are displayed as password dots instead of themselves. Possible value either "true" or "false".
android:phoneNumberIf set, specifies that this TextView has a phone number input method. Possible value either "true" or "false".
android:textText to display.
android:textAllCapsPresent the text in ALL CAPS. Possible value either "true" or "false".
android:textColorText color. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
android:textColorHighlightColor of the text selection highlight.
android:textColorHintColor of the hint text. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
android:textIsSelectableIndicates that the content of a non-editable text can be selected. Possible value either "true" or "false".
android:textSizeSize of the text. Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp).
android:textStyleStyle (bold, italic, bolditalic) for the text. You can use or more of the following values separated by '|'.
  • normal - 0

  • bold - 1

  • italic - 2

android:typefaceTypeface (normal, sans, serif, monospace) for the text. You can use or more of the following values separated by '|'.
  • normal - 0

  • sans - 1

  • serif - 2

  • monospace - 3


This example will take you through simple steps to show how to create your own Android application using Linear Layout and TextView.

1You will use Eclipse IDE to create an Android application and name it as GUIDemo under a package com.example.guidemo as explained in the Hello World Example chapter.
2Modify src/ file to add a click event.
2Modify the detault content of res/layout/activity_main.xml file to include Android UI control.
3Define required constants in res/values/strings.xml file
4Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/com.example.guidemo/ This file can include each of the fundamental lifecycle methods.

package com.example.guidemo;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        //--- text view---
        TextView txtView = (TextView) findViewById(;
        final String Label = txtView.getText().toString();
        txtView.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               "You have clicked the Label : " + Label,


    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

Following will be the content of res/layout/activity_main.xml file:

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="@string/hello_world" />


Following will be the content of res/values/strings.xml to define two new constants:

<?xml version="1.0" encoding="utf-8"?>

    <string name="app_name">GUIDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>


Following is the default content of AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="17" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


Let's try to run your GUIDemo application. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window:

Android textView Control

Now let's click on the Lable "Hello World", it will give following screen:

Android textView Control


I will recommend to try above example with different attributes of TextView in Layout XML file as well at programming time to have different look and feel of the TextView. Try to make it editable, change to font color, font family, width, textSize etc and see the result. You can also try above example with multiple TextView controls in one activity.