Android TextVew in Kotlin

15-Sep-2024

Tutorial for displaying text in Android Studio. How to use TextView.

Here is the tutorial for Java


In Android, a TextView is a basic user interface element used to display text to the user.
It is part of the Android widget framework and can be used programmatically in XML layout files or Java/Kotlin code.
Here's a basic overview of using TextView:


There Are Some Common Attributes for TextView :

id
This is unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
textSize
Change The Text Size
textColor
Change Text Color
drawableLeft
Set Icon in TextView's Left
drawableRightSet Icon in TextView's Right
drawableTopSet Icon in TextView's Top
drawableBotomSet Icon in TextView's Botom
textStyle
bold, italic, normal -> Text Style Change
background
Set background Color


Step 1:

File open res/layout/activity_main.xml file −


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<!--Text Center-->
<TextView
android:id="@+id/idTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Micro Tutorial"
android:gravity="center"
/>

<!--Text textStyle-->
<TextView
android:id="@+id/idTextSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Micro Tutorial"
android:textSize="30dp"
android:gravity="center"
/>

<!--Text Style-->
<TextView
android:id="@+id/idTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Micro Tutorial"
android:textSize="30dp"
android:textStyle="italic|bold"
android:gravity="center"
/>
<!--Text Color-->
<TextView
android:id="@+id/idTextColor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Micro Tutorial"
android:textSize="30dp"
android:textStyle="italic|bold"
android:textColor="#F40303"
android:gravity="center"
/>
<!--Text Margin Padding-->
<TextView
android:id="@+id/idTextChange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="20dp"
android:textSize="30dp"
android:text="Change Text"
android:gravity="center"
/>
</LinearLayout>

</RelativeLayout>


Step 2:

File open com.microappvalley.newtestproject/MainActivity.kt file −


package com.microappvalley.newtestproject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val idTextChange:TextView=findViewById(R.id.idTextChange)

idTextChange.text="Micro Tutorial Text Change"
}
}




Output:


Comments