CHAPTER 16
Beginner
User Input, Forms, and Validation
Updated: May 16, 2026
20 min read
# Chapter 16: User Input, Forms, and Validation
1. Introduction
Forms are a crucial part of any application, from login screens and user profiles to e-commerce checkouts. In Android, building forms involves using components likeEditText, CheckBox, RadioButton, and Switch. Ensuring that the data users enter is accurate and secure requires robust input validation.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use various input controls like
EditText,CheckBox, andSpinner.
-
Group radio buttons using
RadioGroup.
- Extract and process user input from UI components.
- Implement form validation logic to ensure data integrity.
- Provide visual feedback for validation errors.
3. Core Concepts & Implementation
The Input Controls
Android provides several widgets for user input:-
EditText: For text input (passwords, emails, phone numbers).
-
CheckBox: For multiple-choice selections or boolean toggles (e.g., "Terms and Conditions").
-
RadioGroup&RadioButton: For single-choice selections from a list.
-
Switch: For simple on/off states (e.g., enabling notifications).
Building a Simple Registration Form (XML)
Here is a layout for a basic form:
xml
*Note on inputType: This attribute changes the keyboard layout (e.g., showing the '@' symbol for emails).*
Validating Input in Kotlin
When the user clicks the "Register" button, we need to extract the text, check if it meets our criteria, and show an error if it doesn't.
kotlin
Explanation of Validation Logic
-
1.
.trim(): Removes leading and trailing whitespaces.
-
2.
.error: A built-in property ofEditTextthat displays a red warning icon and an error message popup when clicked.
-
3.
.requestFocus(): Moves the cursor to the field containing the error so the user can fix it immediately.
-
4.
Patterns.EMAIL_ADDRESS: A built-in Android utility that uses a standard Regular Expression (Regex) to verify if a string looks like an email.
4. Best Practices
-
Validate Early, Validate Often: Provide real-time validation feedback where possible (e.g., using
TextWatcher).
-
Use Appropriate Input Types: Always use
android:inputType="textEmailAddress",number, ortextPasswordto provide the correct keyboard to the user.
- Never Trust User Input: Validate on the client side (Android app) for user experience, but *always* validate again on your backend server for security.
- Clear Error Messages: Tell the user exactly what went wrong and how to fix it (e.g., "Password must contain a number").
5. Exercises
- 1. Create a "Login" form with Email and Password. Validate that both fields are non-empty before allowing a login attempt.
-
2.
Add a
RadioGroupwith "Male", "Female", and "Other" options to the registration form. Validate that an option has been selected before submission.
6. Coding Challenge
The Feedback Form Create an application with a Feedback Form. It should contain:-
A Rating (using a
RatingBaror aSpinnerwith numbers 1-5).
-
A Multi-line
EditTextfor comments (must be at least 20 characters long).
-
An "Anonymous"
Switch. If the switch is OFF, require the user to enter their Name. If ON, the Name field should be hidden or disabled.
7. Multiple Choice Questions (MCQs)
- 1. Which XML attribute changes the on-screen keyboard to show numbers only?
-
a)
android:keyboard="numbers"
-
b)
android:inputType="number"
-
c)
android:textFormat="integer"
-
d)
android:input="digits"
-
2.
How do you display a built-in error message popup directly on an
EditTextin Kotlin?
-
a)
editText.setError("Message")
-
b)
editText.showError("Message")
-
c)
editText.error = "Message"
- d) Both a and c
- 3. Which built-in utility checks if an email format is valid?
-
a)
EmailValidator.isValid(email)
-
b)
Patterns.EMAIL_ADDRESS.matcher(email).matches()
-
c)
String.isEmail()
-
d)
Android.util.Email.verify()
- 4. What is the main difference between a CheckBox and a RadioButton?
- a) CheckBoxes are round, RadioButtons are square.
- b) CheckBoxes are for single-choice, RadioButtons are for multiple-choice.
- c) RadioButtons within a RadioGroup only allow a single selection, while multiple CheckBoxes can be checked simultaneously.
- d) RadioButtons cannot trigger events.
8. Interview Questions
-
1.
How do you listen for text changes in an
EditTextin real-time?
TextWatcher to the EditText using addTextChangedListener. This provides callbacks for beforeTextChanged, onTextChanged, and afterTextChanged.
-
2.
What is the purpose of
android:inputType?
- 3. How do you hide the soft keyboard programmatically after a user clicks "Submit"?
InputMethodManager system service to hide the soft input from the window token of the currently focused view.