Skip to main content
Android Development with Kotlin
CHAPTER 11 Beginner

TextView, Buttons, Images, and Input Fields

Updated: May 16, 2026
20 min read

# CHAPTER 11

TextView, Buttons, Images, and Input Fields

1. Introduction

The Android SDK provides dozens of native UI components, but 90% of your application's interface will be built using just four fundamental building blocks: Text, Buttons, Images, and Input fields. Mastering these four views allows you to build login screens, profiles, messaging chats, and settings menus. In this chapter, we will master TextView, Buttons, Images, and Input Fields. We will configure their visual attributes in XML and write the Kotlin logic required to make them interactive and responsive to user input.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Render typography utilizing the TextView component.
  • Configure image display algorithms utilizing the ImageView.
  • Capture user keyboard data utilizing EditText and Input Types.
  • Execute logic via interactive Button components.
  • Connect XML Views to Kotlin logic utilizing findViewById and Click Listeners.

3. The TextView (Typography)

A TextView displays read-only text. It is used for titles, paragraphs, and labels.

XML Implementation:

xml
123456789
<TextView
    android:id="@+id/titleText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Create Account"
    android:textSize="24sp"
    android:textStyle="bold"
    android:textColor="#333333"
    android:textAlignment="center" />

*Note the android:id attribute! This is incredibly important. Giving the view an ID (@+id/...) allows our Kotlin code to find it later!*

4. The ImageView (Graphics)

An ImageView displays pictures. Images must be placed in the res/drawable folder.

XML Implementation:

xml
123456
<ImageView
    android:id="@+id/profileImage"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:src="@drawable/my_logo"
    android:scaleType="centerCrop" />

*The scaleType is critical. centerCrop tells the image to fill the 150dp square perfectly, cropping off any excess edges so it doesn't stretch and look weird.*

5. The EditText (User Input)

An EditText is a text box the user can tap to open the keyboard and type into.

XML Implementation:

xml
123456
<EditText
    android:id="@+id/passwordInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter your password"
    android:inputType="textPassword" />

*The android:hint displays faded placeholder text. The inputType="textPassword" is magical: it automatically hides the user's typing with black dots! Other types include number, textEmailAddress, and phone.*

6. The Button (Interactivity)

A Button is meant to be pressed.

XML Implementation:

xml
12345
<Button
    android:id="@+id/submitBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Login" />

7. Connecting XML to Kotlin (The Click Listener)

How do we make the button actually *do* something when tapped? We must connect the XML ID to our Kotlin file.

*(Note: While ViewBinding is the modern standard, findViewById is the architectural foundation every Android developer must understand first).*

Kotlin Logic (MainActivity.kt):

kotlin
1234567891011121314151617181920212223242526272829
package com.example.uibasics

import android.os.Bundle
import android.widget.Button
import android.widget.EditText
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)

        // 1. Find the Views in the XML using their IDs!
        val titleText: TextView = findViewById(R.id.titleText)
        val passwordInput: EditText = findViewById(R.id.passwordInput)
        val submitBtn: Button = findViewById(R.id.submitBtn)

        // 2. Attach a "Listener" that waits for the user to tap the button!
        submitBtn.setOnClickListener {
            // 3. Extract the typed text from the EditText
            val enteredPassword = passwordInput.text.toString()

            // 4. Update the TextView!
            titleText.text = "Password Saved: $enteredPassword"
        }
    }
}

8. Mini Project: Interactive Login Box

Let's build a mini-app where a user types their name, clicks submit, and the screen welcomes them!

The XML Layout:

xml
1234567891011121314151617181920212223242526272829
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    android:gravity="center">

    <TextView
        android:id="@+id/welcomeLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Who are you?"
        android:textSize="22sp" />

    <EditText
        android:id="@+id/nameField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Type your name"
        android:layout_marginTop="20dp" />

    <Button
        android:id="@+id/greetButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Say Hello"
        android:layout_marginTop="20dp" />

</LinearLayout>

The Kotlin Logic:

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

    val welcomeLabel = findViewById<TextView>(R.id.welcomeLabel)
    val nameField = findViewById<EditText>(R.id.nameField)
    val greetButton = findViewById<Button>(R.id.greetButton)

    greetButton.setOnClickListener {
        val userName = nameField.text.toString()
        // If the box isn't empty, update the label!
        if (userName.isNotEmpty()) {
            welcomeLabel.text = "Hello, $userName!"
        }
    }
}

9. Common Mistakes

  • Forgetting .toString(): When you extract text from an EditText (e.g., inputField.text), it does not return a simple String. It returns an Editable object. If you try to compare it (if (inputField.text == "Password")), it will fail. You MUST call .toString() first!
  • NullPointerExceptions with IDs: If you type findViewById(R.id.submitBtn), but you accidentally forgot to add android:id="@+id/submitBtn" into the XML, your app will crash instantly upon opening. The Kotlin logic cannot find the XML button!

10. Best Practices

  • Leverage inputType: Always specify the inputType on an EditText. If you are asking for an age, set inputType="number". This magically forces the phone to open a numeric keypad instead of the full alphabet keyboard, providing a vastly superior User Experience (UX).

11. Exercises

  1. 1. Create an ImageView in XML. Apply an android:src tag pointing to an image you dragged into your drawable folder.
  1. 2. In Kotlin, use findViewById to hook up a Button, and write a setOnClickListener block that prints a message to the Logcat console when tapped.

12. Coding Challenges

Challenge: Enhance the Mini Project. Add a second EditText for the user's Age (ensure it opens a number keypad). When the greetButton is clicked, extract both the Name and the Age. Update the welcomeLabel to say: "Hello [Name], you are [Age] years old!".

13. MCQ Quiz with Answers

Question 1

In Android UI XML, what is the explicit function of the android:id="@+id/my_button" attribute?

Question 2

When extracting user data from an EditText component via Kotlin, why must the .toString() method be appended (e.g., editText.text.toString())?

14. Interview Questions

  • Q: Explain the mechanical flow of event-driven programming utilizing a setOnClickListener. How does the Android main thread know when to execute the closure?
  • Q: Describe the UX impact of the android:inputType attribute on an EditText. Provide three examples of distinct input types and how they mutate the OS soft-keyboard.
  • Q: Contrast the android:src and android:background attributes when applied to an ImageView. Why does applying an image to the background often cause destructive stretching?

15. FAQs

Q: I keep hearing about "ViewBinding". Should I be using findViewById? A: findViewById is the historical foundation. It is perfectly fine for small apps, but it is considered slow and dangerous because it can return null. Modern Android development heavily utilizes "ViewBinding," a Gradle feature that automatically creates safe Kotlin references for all your XML layouts. You will encounter both in the industry.

16. Summary

In Chapter 11, we brought our static XML layouts to life. We implemented TextViews for clean typography, rendered graphics using ImageViews, and built data-entry portals utilizing EditTexts configured with precise OS soft-keyboards. Most importantly, we bridged the gap between visual markup and business logic. We utilized the R.id registry to locate XML components via findViewById, and engineered interactive event-driven architectures utilizing Click Listeners to execute Kotlin operations upon user interaction.

17. Next Chapter Recommendation

Our components look great, but right now they just stack on top of each other. If we want complex grids, overlapping images, and flexible screens, we need advanced container management. Proceed to Chapter 12: Layouts in Android.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·