Android Textview Widget Tutorial With Example

In this tutorial, we will learn about Android Textview Widget. We will also learn about different attributes used to configure the basic class of Textview widget.

Output

Tutorialwing Android TextView Tutorial Output  Android TextView Widget Tutorial

Android TextView Output

Video Output

Getting Started

We can define Android Textview widget as below –

Android Textview is user interface element that shows texts to the user and optionally allow them to edit it. However, Text editing is disabled by default. We need to configure the basic class by adding some attributes to make it editable.

Different Attributes of Android TextView Widget

Below are the different attributes of android Textview widget that are commonly used to customise the Textview. However, you can check the android official documentation website for complete list of Textview attributes. Here, we are listing some of the attributes that we will need commonly.

Sr. XML Attributes Description
1 android:autoLink Use this attribute when you want to automatically detect urls or emails and show it as clickable link.
2 android:autoText If it is set, it means Textview has a textual input method and automatically corrects some common spelling errors.
3 android:capitalize If it is set, it means Textview has a textual input method and should automatically capitalize what the user types.
4 android:id This is unique id of the widget to uniquely identify the widget.
5 android:cursorVisible This is used to make cursor visible or invisible. Default is false.
6 android:drawableBottom Use this attribute when you want to show any drawable(images etc.) below the text.
7 android:drawableEnd Use this attribute when you want to show any drawable(images etc.) to end of the text.
8 android:drawableLeft Use this attribute when you want to show any drawable(images etc.) to left of the text.
9 android:drawablePadding Use this attribute when you want to add padding to the drawable(images etc.).
10 android:drawableRight Use this attribute when you want to show any drawable(images etc.) to right of the text.
11 android:drawableStart Use this attribute when you want to show any drawable(images etc.) to start of the text.
12 android:drawableTop Use this attribute when you want to show any drawable(images etc.) to top of the text.
13 android:ellipsize This attribute causes the text, longer than the view, to be ellipsized at the end.
14 android:ems Makes the Textview be exactly this many ems wide.
15 android:gravity This is used to align the text (by x-axis, y-axis or both) within this Textview.
16 android:height This is used to provide the height to the Textview.
17 android:hint Hint to be shown when there is no text in the Textview.
18 android:inputType This is used to define what are the types of data can be entered by the user for this Textview. For example, Phone, Password, Number, Date, Time etc.
19 android:lines If you want to set height of the Textview by number of lines, you can do it using this attribute. For example, android:lines=”2”, it means height of Textview will be 2 lines.
20 android:maxHeight Use to set the maximum height of the Textview.
21 android:minHeight Use to set the minimum height of the Textview.
22 android:maxLength Use to set the maximum character length of the Textview.
23 android:maxLines Use to set the maximum lines Textview can have.
24 android:minLines Use to set the minimum lines Textview can have.
25 android:maxWidth Use to set the maximum width Textview can have.
26 android:minWidth Use to set the minimum width Textview can have
27 android:text Use to set the text of the Textview
28 android:textAllCaps Use this attribute when you want to show text in capital letters. It takes true or false value only. Default is false.
29 android:textColor Use to set the color of the text.
30 android:textSize Use to set the size of the text.
31 android:textStyle Use to set the style of the text. For example, bold, italic, bolditalic.
32 android:typeface Use to set typeface of the text. For example, normal, sans, serif, monospace.
33 android:width Use to set width of the TextView.



Example Of Android TextView Widget

In this section, you will learn how to use Android Textview. Follow the steps below to create android project. After that, we will use TextView in it. Please ignore the steps if you already know how to create a new android project.

1. Creating New Project

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as TextView. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify the xml and java files to use Textview. Please follow the steps below.

2. Modify values folder

Open res/values/strings.xml file. Add below code into it.

<resources>
   <string name="app_name">TextView</string>
   <string name="click_on_me">Click On Me</string>
   <string name="clicked_on_me">You clicked on me.</string>
</resources>

Other values folders have not been changed. So, we are not going to mention it here.

3. Use TextView in xml file

Open res/layout/activity_main.xml file. Add below code into it. You may also visit post to Use Textview Widget programmatically in Android

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/activity_main"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center">

	<TextView
		android:id="@+id/textView"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/click_on_me"/>
</LinearLayout>

Note that we have added TextView widget in this xml file.

Now, we will access this TextView in java file and perform some action.

4. Access TextView in Java File

Open src/main/java/com.tutorialwing.textview/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.textview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TextView textView = (TextView) findViewById(R.id.textView);
		if (textView != null) {
			textView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Toast.makeText(MainActivity.this, R.string.clicked_on_me, Toast.LENGTH_LONG).show();
				}
			});
		}
	}
}

In this java file, we are showing a Toast message on click of TextView widget. However, you can perform any action you need.

4. AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.textview" 
        xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

Finally, when you run the app, you will get output as shown above.

Leave a Reply