
- Android Basics
- Android - Home
- Android - Overview
- Android - Environment Setup
- Android - Architecture
- Android - Application Components
- Android - Hello World Example
- Android - Resources
- Android - Activities
- Android - Services
- Android - Broadcast Receivers
- Android - Content Providers
- Android - Fragments
- Android - Intents/Filters
- Android - User Interface
- Android - UI Layouts
- Android - UI Controls
- Android - Event Handling
- Android - Styles and Themes
- Android - Custom Components
- Android Advanced Concepts
- Android - Drag and Drop
- Android - Notifications
- Location Based Services
- Android - Sending Email
- Android - Sending SMS
- Android - Phone Calls
- Publishing Android Application
- Android Useful Examples
- Android - Alert Dialoges
- Android - Animations
- Android - Audio Capture
- Android - AudioManager
- Android - Auto Complete
- Android - Best Practices
- Android - Bluetooth
- Android - Camera
- Android - Clipboard
- Android - Custom Fonts
- Android - Data Backup
- Android - Developer Tools
- Android - Emulator
- Android - Facebook Integration
- Android - Gestures
- Android - Google Maps
- Android - Image Effects
- Android - ImageSwitcher
- Android - Internal Storage
- Android - JetPlayer
- Android - JSON Parser
- Android - Linkedin Integration
- Android - Loading Spinner
- Android - Localization
- Android - Login Screen
- Android - MediaPlayer
- Android - Multitouch
- Android - Navigation
- Android - Network Connection
- Android - NFC Guide
- Android - PHP/MySQL
- Android - Progress Circle
- Android - ProgressBar
- Android - Push Notification
- Android - RenderScript
- Android - RSS Reader
- Android - Screen Cast
- Android - SDK Manager
- Android - Sensors
- Android - Session Management
- Android - Shared Preferences
- Android - SIP Protocol
- Android - Spelling Checker
- Android - SQLite Database
- Android - Support Library
- Android - Testing
- Android - Text to Speech
- Android - TextureView
- Android - Twitter Integration
- Android - UI Design
- Android - UI Patterns
- Android - UI Testing
- Android - WebView Layout
- Android - Wi-Fi
- Android - Widgets
- Android - XML Parsers
- Android Useful Resources
- Android - Questions and Answers
- Android - Useful Resources
- Android - Discussion
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.
Sr.No. | Attribute & Description |
---|---|
1 |
android:id This is the ID which uniquely identifies the control. |
2 |
android:capitalize If set, specifies that this TextView has a textual input method and should automatically capitalize what the user types.
|
3 |
android:cursorVisible Makes the cursor visible (the default) or invisible. Default is false. |
4 |
android:editable If set to true, specifies that this TextView has an input method. |
5 |
android:fontFamily Font family (named by string) for the text. |
6 |
android:gravity Specifies how to align the text by the view's x- and/or y-axis when the text is smaller than the view. |
7 |
android:hint Hint text to display when the text is empty. |
8 |
android:inputType The type of data being placed in a text field. Phone, Date, Time, Number, Password etc. |
9 |
android:maxHeight Makes the TextView be at most this many pixels tall. |
10 |
android:maxWidth Makes the TextView be at most this many pixels wide. |
11 |
android:minHeight Makes the TextView be at least this many pixels tall. |
12 |
android:minWidth Makes the TextView be at least this many pixels wide. |
13 |
android:password Whether the characters of the field are displayed as password dots instead of themselves. Possible value either "true" or "false". |
14 |
android:phoneNumber If set, specifies that this TextView has a phone number input method. Possible value either "true" or "false". |
15 |
android:text Text to display. |
16 |
android:textAllCaps Present the text in ALL CAPS. Possible value either "true" or "false". |
17 |
android:textColor Text color. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". |
18 |
android:textColorHighlight Color of the text selection highlight. |
19 |
android:textColorHint Color of the hint text. May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb". |
20 |
android:textIsSelectable Indicates that the content of a non-editable text can be selected. Possible value either "true" or "false". |
21 |
android:textSize Size of the text. Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp). |
22 |
android:textStyle Style (bold, italic, bolditalic) for the text. You can use or more of the following values separated by '|'.
|
23 |
android:typeface Typeface (normal, sans, serif, monospace) for the text. You can use or more of the following values separated by '|'.
|
Example
This example will take you through simple steps to show how to create your own Android application using Linear Layout and TextView.
Step | Description |
---|---|
1 | You will use Android studio to create an Android application and name it as demo under a package com.example.demo as explained in the Hello World Example chapter. |
2 | Modify src/MainActivity.java file to add necessary code . |
2 | Modify the default content of res/layout/activity_main.xml file to include Android UI control. |
3 | No need to change default string constants at string.xml file. Android studio takes care of default string constants. |
4 | Run 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.demo/MainActivity.java. This file can include each of the fundamental lifecycle methods.
package com.example.demo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //--- text view--- TextView txtView = (TextView) findViewById(R.id.text_id); } }
Following will be the content of res/layout/activity_main.xml file −
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/text_id" android:layout_width="300dp" android:layout_height="200dp" android:capitalize="characters" android:text="hello_world" android:textColor="@android:color/holo_blue_dark" android:textColorHighlight="@android:color/primary_text_dark" android:layout_centerVertical="true" android:layout_alignParentEnd="true" android:textSize="50dp"/> </RelativeLayout>
Following will be the content of res/values/strings.xml to define two new constants −
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">demo</string> </resources>
Following is the default content of AndroidManifest.xml −
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name="com.example.demo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Let's try to run your demo application. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project's activity files and click Run icon from the toolbar. Android studio 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 −

Exercise
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.