TextView in Kotlin
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:
- Add a TextView in activity_main.xml file inside LinearLayout.
- Add attributes like text, textColor, textSize, textStyle in the activity_main.xml file.
- 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" >w3wiki</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:android = "http://schemas.android.com/apk/res/android" 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.w3wiki.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" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.w3wiki.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 > |
Contact Us