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. This is lesson 4 of 7. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 7 lessons in total.
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 secondsToast.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@keynumberDecimal— numeric keyboardtextPassword— hides charactersphone— 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 inputToast.makeText(...).show()— quick feedback messagesinputType— hint the right keyboard to users
Next: navigate between screens with Intents.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 36
- Lessons
- 152
Frequently Asked Questions
Is the “Handling User Input” lesson free?
Yes — any 3 lessons of the Android Academy learning path, including “Handling User Input”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 7 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 7.
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
- Project Structure & Manifest
- Activities & Lifecycle
- Layouts & Views
- Handling User Input
- Intents & Navigation
- Fragments
- Permissions & Runtime Requests