0Pricing
Android Academy · Lesson

Handling User Input

Respond to button clicks, read text from EditText, validate input, and provide user feedback with Toast messages.

Handling User Input is a free Android Academy lesson on CoddyKit — lesson 4 of 7. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 7 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Connecting UI to Code

Building a layout is just the first step. You also need to:

  • Read what the user typed in an EditText
  • Respond to button clicks
  • Update TextViews with new values

This lesson shows how to wire up UI elements using ViewBinding — the modern, type-safe approach.

Enabling ViewBinding

Enable ViewBinding in your app/build.gradle:

// app/build.gradle (inside android { })
android {
    ...
    buildFeatures {
        viewBinding = true
    }
}

Using ViewBinding

ViewBinding generates a binding class from your XML layout. If the layout is activity_main.xml, the class is ActivityMainBinding.

import androidx.appcompat.app.AppCompatActivity
import com.example.myapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // Access views directly — no casting!
        binding.tvTitle.text = "Hello!"
    }
}

Handling Button Clicks

Set a click listener on any view using setOnClickListener. Pass a lambda that runs when the user taps:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)

    var count = 0
    binding.btnIncrement.setOnClickListener {
        count++
        binding.tvCount.text = "Count: $count"
    }

    binding.btnReset.setOnClickListener {
        count = 0
        binding.tvCount.text = "Count: 0"
    }
}

Reading EditText Input

Get the text a user typed using .text.toString():

  • binding.etName.text.toString() — returns the current text as a String
  • Always trim whitespace: .trim()
  • Check for empty: use .isBlank() or .isEmpty()

EditText + Button Example

Read input and display it when a button is clicked:

binding.btnGreet.setOnClickListener {
    val name = binding.etName.text.toString().trim()

    if (name.isBlank()) {
        binding.tvResult.text = "Please enter your name"
        return@setOnClickListener
    }

    binding.tvResult.text = "Hello, $name!"
}

Toast Messages

A Toast is a small pop-up message shown briefly at the bottom of the screen. Use it for quick feedback:

  • Toast.LENGTH_SHORT — ~2 seconds
  • Toast.LENGTH_LONG — ~3.5 seconds

Toast Example

Show a Toast from a button click:

import android.widget.Toast

binding.btnSave.setOnClickListener {
    val input = binding.etEmail.text.toString().trim()

    if (input.contains("@")) {
        Toast.makeText(
            this,
            "Saved: $input",
            Toast.LENGTH_SHORT
        ).show()
    } else {
        Toast.makeText(
            this,
            "Invalid email address",
            Toast.LENGTH_LONG
        ).show()
    }
}

Input Types & Keyboard

Set the inputType attribute on EditText to hint the keyboard:

  • textEmailAddress — shows @ key
  • numberDecimal — numeric keyboard
  • textPassword — hides characters
  • phone — phone number keypad

Good input types improve UX significantly.

Quick Check

Which method reads the current text from an EditText as a String?

Recap: Handling User Input

Your apps can now interact with users:

  • Enable ViewBinding for type-safe view access
  • setOnClickListener { } — respond to taps
  • .text.toString().trim() — read EditText input
  • Toast.makeText(...).show() — quick feedback messages
  • inputType — hint the right keyboard to users

Next: navigate between screens with Intents.

Frequently asked questions

Is the “Handling User Input” lesson free?

Yes — the full text of “Handling User Input” is free to read here on the web, and the Android Academy course includes 7 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Handling User Input”?

Respond to button clicks, read text from EditText, validate input, and provide user feedback with Toast messages. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 7, so you can start here or from the beginning and move at your own pace.

How long does the “Handling User Input” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Project Structure & Manifest
  2. Activities & Lifecycle
  3. Layouts & Views
  4. Handling User Input
  5. Intents & Navigation
  6. Fragments
  7. Permissions & Runtime Requests
← Back to Android Academy