The Wayback Machine - https://web.archive.org/web/20240930154416/https://www.geeksforgeeks.org/textview-in-kotlin/
Open In App

TextView in Kotlin

Last Updated : 29 Oct, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Android TextView is simply a view that are used to display the text to the user and optionally allow us to modify or edit it. First of all, open Kotlin project in Android Studio.
Following steps are used to create TextView in Kotlin:

  1. Add a TextView in activity_main.xml file inside LinearLayout.
  2. Add attributes like text, textColor, textSize, textStyle in the activity_main.xml file.
  3. Open MainActivity.kt file and set OnClickListener for the textView to show the Toast message.

Different attributes of TextView widgets –

Attributes Description
android:text Sets text of the Textview
android:id Gives a unique ID to the Textview
android:cursorVisible Use this attribute to make cursor visible or invisible. Default value is visible.
android:drawableBottom Sets images or other graphic assets to below of the Textview.
android:drawableEnd Sets images or other graphic assets to end of Textview.
android:drawableLeft Sets images or other graphic assets to left of Textview.
android:drawablePadding Sets padding to the drawable(images or other graphic assets) in the Textview.
android:autoLink This attribute is used to automatically detect url or emails and show it as clickable link.
android:autoText Automatically correct spelling errors in text of the Textview.
android:capitalize It automatically capitalize whatever the user types in the Textview.
android:drawableRight Sets drawables to right of text in the Textview.
android:drawableStart Sets drawables to start of text in the Textview.
android:drawableTop Sets drawables to top of text in the Textview.
android:ellipsize Use this attribute when you want text to be ellipsized if it is longer than the Textview width.
android:ems Sets width of the Textview in ems.
android:gravity We can align text of the Textview vertically or horizontally or both.
android:height Use to set height of the Textview.
android:hint Use to show hint when there is no text.
android:inputType Use to set input type of the Textview. It can be Number, Password, Phone etc.
android:lines Use to set height of the Textview by number of lines.
android:maxHeight Sets maximum height of the Textview.
android:minHeight Sets minimum height of the Textview.
android:maxLength Sets maximum character length of the Textview.
android:maxLines Sets maximum lines Textview can have.
android:minLines Sets minimum lines Textview can have.
android:maxWidth Sets maximum width Textview can have.
android:minWidth Sets minimum lines Textview can have.
android:textAllCaps Show all texts of the Textview in capital letters.
android:textColor Sets color of the text.
android:textSize Sets font size of the text.
android:textStyle Sets style of the text. For example, bold, italic, bolditalic.
android:typeface Sets typeface or font of the text. For example, normal, sans, serif etc
android:width Sets width of the TextView.

Modify the strings.xml file

We can add strings in the strings.xml file and use in the other files easily by calling them with their names.




<resources>
    <string name="app_name">TextViewInKotlin</string>
    <string name="text_view">GeeksForGeeks</string>
    <string name="text_on_click">COMPUTER SCIENCE PORTAL</string>
</resources>


activity_main.xml file

Open activity_main.xml file and create a TextView using id textView.




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--EditText with id editText-->
  
    <TextView
        android:id="@+id/text_view_id"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/text_view"
        android:textColor="#008000"
        android:textSize="40dp"
        android:textStyle="bold"/>
</LinearLayout>


Open MainActivity.kt file and get the reference of TextView defined in the layout file.

  
// finding the textView
 val textView = findViewById(R.id.text_view_id) as TextView

Setting the on click listener to the button

textView?.setOnClickListener{ Toast.makeText(this@MainActivity,
                "COMPUTER SCIENCE PORTAL", Toast.LENGTH_LONG).show() }

MainActivity.kt file

Open app/src/main/java/yourPackageName/MainActivity.kt to get the reference of TextView.




package com.geeksforgeeks.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
  
class MainActivity : AppCompatActivity() {
  
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
  
            //accessing our textview from layout
            val textView = findViewById<TextView>(R.id.text_view_id) as TextView
            textView?.setOnClickListener{ Toast.makeText(this@MainActivity,
                R.string.text_on_click, Toast.LENGTH_LONG).show() }
        }
  
}


AndroidManifest.xml file

We are also going to see the code inside main/AndroidManifest.xml file.




<?xml version="1.0" encoding="utf-8"?>
    package="com.geeksforgeeks.myfirstkotlinapp">
  
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>


Run as emulator for output:



Previous Article
Next Article

Similar Reads

Dynamic TextView in Kotlin
Android TextView is an user interface that is used to display some text to the user. In this article we will be discussing how to programmatically create a TextView in Kotlin . Let’s start by first creating a project in Android Studio. To do so, follow these instructions: Click on File, then New and then New Project and give name whatever you like
2 min read
Create Clickable Hyperlinks in TextView in Android with Kotlin
TextView is used to display text in Android. text can be characters, numbers, strings, and paragraphs. We can also display web URLs in the TextView. However, these links are non-clickable and cannot be used for website navigation. Moreover, we cannot create hyperlinks directly into the TextView. So in this article, we will show you a correct way of
2 min read
How to add fading TextView animation in Android
TextView is the basic building block of user interface components. It is used to set the text and display it to the user. It is a very basic component and used a lot. A Fading TextView is a TextView that changes its content automatically every few seconds. If we want to design a beautiful interface than we can use Fading TextView. Approach: Add thi
1 min read
How to Display HTML in Textview along with Images in Android?
There are some situations when one needs to display rich formatted text in the app, like for a blog app or an app like Quora but Android's inbuilt function doesn't allow to display inline images by default, moreover they show the ugly blue line to the left of anything inside blockquote tag. Here is the simple solution to show HTML in TextView along
11 min read
How to Add a TextView with Rounded Corner in Android?
TextView is an essential object of an Android application. It comes in the list of some basic objects of android and used to print text on the screen. In order to create better apps, we should learn that how can we create a TextView which have a background with rounded corners. We can implement this skill in a practical manner for any android appli
2 min read
How to Make TextView and EditText Selectable in Android?
In this article, we are going to implement a very important feature related to TextView. While using any social media app or like using Facebook you may have seen there is a particular type of TextView which you cannot copy like a caption that people write on their posts. You can select the message but there few texts which cannot select or copy. S
3 min read
How to Make Substring of a TextView Clickable in Android?
In this article, we are going to implement a very important feature related to TextView. Here we are making part of a string or a substring to act as a substring. This feature is important while writing a blog because it may be possible that for certain points we want to redirect the user to a link. So here we are going to learn how to implement th
2 min read
Expandable TextView in Android
ExpandableTextView is an Android library which allows us to easily create a TextView which can expand/collapse when user clicks on it .we can use this feature in many apps such as movie review app or storytelling app and in many other apps. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are go
4 min read
How to Create Blink Effect on TextView in Android?
In this article, we are going to implement a very important feature related to TextView. Here we are adding the blink text feature on a TextView. This feature can be used to show important announcements or notifications in an App. Even we can add this feature to show important links for the user. So here we are going to learn how to implement that
2 min read
How to Create Dashed Underline under TextView in Android?
In this article, we will explain how to create a dashed underline below a text view in android. A sample image is shown below to give an idea of what we are going to build. Step by Step Implementation Step 1: Create a new project To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Step 2: W
2 min read
How to Add Hyperlink to TextView in Android?
Hello geeks, today we are going to make an application where we will see that how we can add a link to a TextView in Android, and using this concept we will add portals - home and practice (of GeeksforGeeks) in our application. So that, user can directly go to these portals from our application. What we are going to build in this article? We will b
3 min read
How to Change Typeface of TextView in Android?
A typeface is a particular design for alphabets that separates it from other typefaces in terms of style, size, and weight variations. In general, there are a lot of local typefaces available on your device or software for use. However, many more typefaces are available on the Internet that can be downloaded and used for respective works. Similarly
2 min read
How to Change TextView Size Programmatically in Android?
A TextView in Android is a UI element to display text. It can be programmed in the layout file statically as well as in the main code dynamically. Thus, various attributes of a TextView such as the text, text color, text size, TextView background, and its size can be changed programmatically. In this article, we will show you how you could programm
3 min read
How to Underline Text in TextView in Android?
In Android, a TextView is a primary UI element used to display text present in the form of characters, numbers, strings, and paragraphs. However, styles cannot be applied to text present in the TextView. So in this article, we will show you how you could underline text in a TextView in Android. Follow the below steps once the IDE is ready. Step by
2 min read
How to Set the Text Color of TextView in Code?
One of the most often used User Interface elements is TextView. The user experience is improved, and the application looks better, due to the clear and organized text. It doesn't matter what kind of information is being shown, it should always be done well. In an android app, TextView allows us to do this, thus understanding its properties is cruci
2 min read
How to Make TextView Scrollable in Android?
In Android, a TextView is a primary UI element used to display text present in the form of numbers, strings, and paragraphs. However, for a large amount of information to be displayed size adjustment doesn't work. So in this article, we will show you how to make TextView scrollable on Android. Follow the below steps once the IDE is ready. TextView
8 min read
TextView in Android with Example
TextView is a simple widget that is seen in every android application. This widget is used to display simple text within the android application. We can add custom styling to the text that we have to show. In this article, we will take a look at How to create a simple Text View in an android application. Note: This Android article covered in both J
2 min read
Android - Center Text in a TextView Horizontally and Vertically
Many android applications use text view for displaying text within android applications. There are different ways used to align the text view within our XML layout. To align TextView for different screen sizes we cannot use margin from all sides. For aligning the text view for different screen sizes we have to align them centrally to vertically and
2 min read
Android - Single TextView with Multiple Colored Text
In this article, we will learn about how to add two or more text colors in a single string of TextView of Android using Kotlin. It is very easy to add multiple colors in a single string, just we have to use the Html library and font tag. The syntax is the same as we write in Html for the text that we want to display on the webpage. Here, we don't h
3 min read
How to Change Android TextView Remotely Through Firebase?
First Of all, let's understand how we are going to do this first we will be using one feature of Firebase which is RemoteConfig, and with the help of that we are going to change the TextView. The basic principle of that you can change the text view of the app without pushing any update which is one of the main advantages of the Remote Config. To le
6 min read
TextView widget in Android using Java with Examples
Widget refers to the elements of the UI (User Interface) that help the user interact with the Android App. TextView is one of many such widgets which can be used to improve the UI of the app. TextView refers to the widget which displays some text on the screen based on the layout, size, colour, etc set for that particular TextView. It optionally al
4 min read
Working With the TextView in Android
TextView in Android is one of the basic and important UI elements. This plays a very important role in the UI experience and depends on how the information is displayed to the user. This TextView widget in Android can be dynamized in various contexts. For example, if the important part of the information is to be highlighted then the substring that
7 min read
Autosizing TextView in Android
If the user is giving the input and the input needs to be shown as TextView and if the user inputs the stuff which can go out of the screen, then in this case the font TextView should be decreased gradually. So, in this article, it has been discussed how the developer can reduce the size of TextView gradually as the user gives the data. One can hav
6 min read
Glow TextView in Android
By default Android doesn't provide a simple way to add the glow to the text view or any other view, so to make our app more attractive and beautiful we can add the glow effect to our TextView. In order to do so, we can use many different external libraries. A sample GIF is given below to get an idea about what we are going to do in this article. No
5 min read
Zoom In and Out to TextView in Android
In this article, we are going to implement the zoom-in and zoom-out feature in TextView. As we zoom we will see the image after zooming in. Basically, we are going to learn how to increase or decrease the text size in Android. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to impleme
2 min read
How to Increase or Decrease TextView Font Size in Android Programmatically?
In this App, we are going to learn how to increase or decrease TextView Font Size in Android programmatically. Like we have seen that in many apps we sometimes want to enlarge the text. So here basically we are going to implement that. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going t
3 min read
Kotlin Data Types
The most fundamental data type in Kotlin is the Primitive data type and all others are reference types like array and string. Java needs to use wrappers (java.lang.Integer) for primitive data types to behave like objects but Kotlin already has all data types as objects. There are different data types in Kotlin: Integer Data typeFloating-point Data
3 min read
Hello World program in Kotlin
Hello, World! is the first basic program in any programming language. Let's write the first program in Kotlin programming language. The "Hello, World!" program in Kotlin: Open your favorite editor notepad or notepad++ and create a file named firstapp.kt with the following code. // Kotlin Hello World Program fun main(args: Array<String>) { pri
2 min read
Android Shared Element Transition with Kotlin
Shared element transition is seen in most of the applications which are used when the user navigates between two screens. This type of animation is seen when a user clicks on the items present in the list and navigates to a different screen. During these transitions, the animation which is displayed is called a shared element transition. In this ar
4 min read
Kotlin | Retrieve Collection Parts
The slice function allows you to extract a list of elements from a collection by specifying the indices of the elements you want to retrieve. The resulting list contains only the elements at the specified indices. The subList function allows you to retrieve a sublist of a collection by specifying the start and end indices of the sublist. The result
5 min read