Android Application Demonstration Using Kotlin: EditText, TextView, and Button

 


Android + Kotlin

Android App Demo Using Kotlin

EditText · TextView · Button — A Step-by-Step Beginner Tutorial

Android app development has become more powerful and developer-friendly since the introduction of Kotlin as an official language for Android by Google in 2017. Kotlin simplifies coding, reduces boilerplate, and enhances readability compared to Java. In this tutorial, we will build a simple Android app using EditText, TextView, and Button.

⚡ Why Use Kotlin for Android?

Officially Supported by Google
🛡️
Null Safety Features
📝
Concise & Readable Syntax
🔗
Interoperable with Java
🚀
Less Boilerplate Code
🏗️
Modern App Architecture

🎯 Project Overview

Build a simple Android application where:

1 User enters their name in an EditText field
2 User presses a Button to submit
3 A greeting message appears in a TextView

📋 Step-by-Step Implementation

1

Create a New Android Project

Open Android Studio → Click New Project → Select Empty Activity → Choose Kotlin as the programming language → Click Finish.

2

Design the Layout — activity_main.xml

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">

    <EditText
        android:id="@+id/editTextName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name" />

    <Button
        android:id="@+id/buttonSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_marginTop="16dp" />

    <TextView
        android:id="@+id/textViewResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_marginTop="20dp" />

</LinearLayout>
3

Write Kotlin Code — MainActivity.kt

MainActivity.kt
package com.example.demoapp

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

class MainActivity : AppCompatActivity() {

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

        val editTextName = findViewById<EditText>(R.id.editTextName)
        val buttonSubmit = findViewById<Button>(R.id.buttonSubmit)
        val textViewResult = findViewById<TextView>(R.id.textViewResult)

        buttonSubmit.setOnClickListener {
            val name = editTextName.text.toString()
            textViewResult.text = "Hello, $name! Welcome to Android."
        }
    }
}

🔍 Understanding Each Component

✏️

EditText

Receives user input. Provides an editable text field. Access via .text.toString()

🔘

Button

Triggers an action. Handles click events using setOnClickListener

📄

TextView

Displays text to the user. Updated dynamically using .text = "..."

⚙️ How the App Works

1

User types their name into the EditText field

2

Button is clicked, triggering setOnClickListener

3

Kotlin retrieves the text using .text.toString()

4

TextView displays the personalized greeting message

💡 Best Practices

🔒Avoid null pointer exceptions
📛Use meaningful variable names
🏛️Follow MVVM architecture
🔗Use View Binding over findViewById
⚖️Keep UI and business logic separate

❓ Frequently Asked Questions

Why is Kotlin preferred over Java for Android?

Kotlin reduces boilerplate code and provides null safety, making development faster and safer compared to Java.

What is EditText in Android?

EditText is a UI component that allows users to enter and edit text, making it essential for form inputs and data collection.

How do you handle button click events in Kotlin?

Using the setOnClickListener method. Inside the lambda, you write the code you want to execute when the button is pressed.

Can I update TextView dynamically?

Yes! Simply assign a new value: textView.text = "New Text" and the UI will update instantly.

Is Android Studio necessary for development?

Yes, Android Studio is the official IDE for Android development, providing all necessary tools including emulator, debugger, and layout editor.

Happy Coding! 🚀

Build great Android apps with Kotlin



Post a Comment

0 Comments