Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.In this tutorial, we will learn about android Textview using Kotlin. You will also learn about different attributes that are used to customise Textview widget.
Output

Android TextView Using Kotlin Output
Video Output
Getting Started
We can define android Textview widget as below –
Android TextView widget is a View that are used to show texts to the user and optionally allow them to edit it. However, Text editing is disabled by default. You need to customise the basic class of TextView Widget to make it editable.
Different Attributes of Android TextView Widget
Below are the various attributes that are used to customise android TextView Widget. However, you can check the complete list of attributes of Textview in it’s official documentation site. Here, we are going to list some of the important attributes of this widget.
Sr. | XML Attributes | Description |
---|---|---|
1 | android:autoLink | This attribute is used to automatically detect url or emails and show it as clickable link. |
2 | android:autoText | Use this attribute if you want to automatically correct spelling errors in text of the Textview. |
3 | android:capitalize | If it is set, it means Textview has a textual input method and should automatically capitalize whatever the user types in the Textview. |
4 | android:id | This attribute is use to provide unique ID to the Textview widget. |
5 | android:cursorVisible | Use this attribute to make cursor visible or invisible. Default is visible. |
6 | android:drawableBottom | Sets drawable(images etc.) below the text of the Textview. |
7 | android:drawableEnd | Sets drawable(images etc.) to end of text in the Textview. |
8 | android:drawableLeft | Sets drawable(images etc.) to left of text in the Textview. |
9 | android:drawablePadding | Sets padding to the drawable(images etc.) in the Textview. |
10 | android:drawableRight | Sets drawable(images etc.) to right of text in the Textview. |
11 | android:drawableStart | Sets drawable(images etc.) to start of text in the Textview. |
12 | android:drawableTop | Sets drawable(images etc.) to top of text in the Textview. |
13 | android:ellipsize | Use this attribute when you want text to be ellipsized if it is longer than the Textview width. |
14 | android:ems | Sets width of the Textview in ems. |
15 | android:gravity | Use to align text of the Textview vertically or horizontally or both. |
16 | android:height | Use to set height of the Textview. |
17 | android:hint | Use to show hint when there is no text. |
18 | android:inputType | Use to set input type of the Textview. It can be Number, Password, Phone etc. |
19 | android:lines | Use to set height of the Textview by number of lines. |
20 | android:maxHeight | Sets maximum height of the Textview. |
21 | android:minHeight | Sets minimum height of the Textview. |
22 | android:maxLength | Sets maximum character length of the Textview. |
23 | android:maxLines | Sets maximum lines Textview can have. |
24 | android:minLines | Sets minimum lines Textview can have. |
25 | android:maxWidth | Sets maximum width Textview can have. |
26 | android:minWidth | Sets minimum lines Textview can have. |
27 | android:text | Sets text of the Textview |
28 | android:textAllCaps | Use this attribute if you want to show all texts of the Textview in capital letters. |
29 | android:textColor | Sets color of the text. |
30 | android:textSize | Sets font size of the text. |
31 | android:textStyle | Sets style of the text. For example, bold, italic, bolditalic. |
32 | android:typeface | Sets typeface of the text. For example, normal, sans, serif, monospace. |
33 | android:width | Sets width of the TextView. |
Example Of Android TextView Using Kotlin
In this section, you will see how to use android TextView using kotlin. Follow the steps below to create new android project. After that, we will use Textview in it. Please ignore the steps if you have already created a new android project.
1. Creating New Project in Kotlin
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as TextView. Then, check Include Kotlin Support and 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. |
You can also have a look at our tutorial on Create Android Project in Android. Steps in this tutorial are similar as discussed above.
Now, we are going to changes the code into xml and kotlin file. So, follow the steps below.
2. Modify Values Folder
Open res/values/strings.xml file and add below code into it.
<resources> <string name="app_name">TextViewUsingKotlin</string> <string name="click_on_me">Click On Me</string> <string name="clicked_on_me">You clicked on me.</string> </resources>
Since 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 create and use Textview programmatically in kotlin
<?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 add TextView widget in this xml file.
Now, we will access this TextView in kotlin file and perform some action.
4. Access TextView in Kotlin File
Since we are done with xml and values folder changes. Now, we will see how to access TextView using kotlin and perform some action on click of it. So, open src/main/java/com.tutorialwing.textview/MainActivity.kt file. Then, add below code into it.
package com.tutorialwing.textview import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.widget.TextView import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView = findViewById(R.id.textView) as TextView textView?.setOnClickListener { Toast.makeText(this@MainActivity, R.string.clicked_on_me, Toast.LENGTH_LONG).show() } } }
In this kotlin file, we are showing a Toast message on click of TextView widget. However, you can perform any action you need.
AndroidManifest.xml file
Since AndroidManifest is very important file in any android application. We are also going to see the code inside main/AndroidManifest.xml file.
<?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.
That’s end of our tutorial on TextView using Kotlin Programming language.