TextView

 

TextView

Imagine a label on a form or title on a screen.
In Android, the component used to display text to the user is called TextView.

Example texts shown using TextView:

Welcome to App
Login
User Name
Product Price

1. Definition

TextView is a basic UI component in Android used to display text on the screen.

Simple meaning:

TextView = A view that shows text to the user.

2. Purpose

TextView is used to:

  • Display titles

  • Show labels

  • Display messages

  • Show data or results

Examples in apps:

App ScreenTextView Usage
Login screenUsername label
Shopping appProduct name
CalculatorResult display
News appArticle title

3. Basic Syntax (XML)

Example in layout file:

<TextView
    android:id="@+id/textTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello Android"/>

Explanation:

  • id → unique identifier

  • layout_width → width

  • layout_height → height

  • text → text displayed


4. Using TextView in Kotlin Code

Inside Activity:

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

class MainActivity : AppCompatActivity() {

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

        val textView = findViewById<TextView>(R.id.textTitle)
        textView.text = "Welcome to Kotlin"
    }
}

Explanation:

  • findViewById() connects Kotlin with XML

  • textView.text changes displayed text


5. Important Properties

1. text

Sets the text displayed.

android:text="Welcome"

2. textSize

Controls text size.

android:textSize="20sp"

3. textColor

Changes text color.

android:textColor="#FF0000"

4. gravity

Aligns text inside TextView.

android:gravity="center"

Options:

center
left
right
top
bottom

5. maxLines

Limits number of lines.

android:maxLines="2"

6. padding

Adds space inside TextView.

android:padding="10dp"

6. Important Methods (Kotlin)

setText()

Change text dynamically.

textView.text = "New Text"

append()

Add text at the end.

textView.append(" Added Text")

setTextColor()

Change text color.

textView.setTextColor(Color.RED)

setTextSize()

Change text size.

textView.textSize = 24f

setVisibility()

Show or hide text.

textView.visibility = View.GONE

7. Real-Time Examples

Example 1: App Title

XML

<TextView
    android:text="My Shopping App"
    android:textSize="24sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Result:

My Shopping App

Example 2: Login Label

Username
Password

Used in forms to identify input fields.


Example 3: Display Result

Calculator result:

Result: 25

Kotlin code:

val resultText = findViewById<TextView>(R.id.txtResult)
resultText.text = "Result: 25"

Example 4: Welcome Message

Welcome, Rahul!

Kotlin example:

textView.text = "Welcome, $username!"

8. Advantages

✔ Simple and lightweight
✔ Easy to customize
✔ Essential for displaying text
✔ Works with dynamic data


9. Disadvantages

❌ Cannot accept user input
(for input we use EditText)


10. One-Line Summary

TextView is an Android UI component used to display text information on the screen.



Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form