Designing Forms and Authentication Screens
# CHAPTER 13
Designing Forms and Authentication Screens
1. Introduction
Forms are often the point of highest friction in any app. Whether it is a login screen, a signup flow, or a checkout page, if the UI is confusing or clunky, users will abandon the app. In Android, the standardEditText widget is functional, but visually bare. To create modern, accessible forms, we use TextInputLayout from the Material Components library. In this chapter, we will design beautiful authentication screens complete with floating labels, error states, and password toggles.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Use
TextInputLayoutto wrap your input fields.
- Customize the style of input fields (Filled vs. Outlined).
- Implement password visibility toggles (the "eye" icon).
- Design clear error states and form validation feedback.
- Build a complete Login/Signup UI.
3. Introducing TextInputLayout
TextInputLayout acts as a wrapper around a TextInputEditText. It provides the modern "floating label" effect: when the user taps the field, the hint text shrinks and floats to the top, making room for the user's input while keeping the context visible.
4. Customizing the Input Fields
Material Design provides two primary styles for input fields:-
FilledBox (
style="...FilledBox"): A solid background with a dark bottom indicator line. Great for forms with many fields.
-
OutlinedBox (
style="...OutlinedBox"): A clean, rounded stroke border around the entire field. Great for simple, high-impact forms like Login screens.
Customizing Colors:
You can change the outline color or the floating label color by customizing attributes like app:boxStrokeColor and app:hintTextColor.
5. Password Fields and Icons
Typing a password on a mobile device is error-prone. You should always provide a way for the user to see what they are typing.TextInputLayout makes this trivial:
You can also add icons to the *start* of the field using app:startIconDrawable="@drawable/ic_lock".
6. Error States and Form Validation
When a user makes a mistake (e.g., entering an invalid email), the UI must clearly communicate the error. Do not rely on "Toast" messages. Display the error directly under the offending field.In XML, enable errors:
app:errorEnabled="true"
In your Kotlin code, set the error text:
When error is set, the outline and the floating label automatically turn red (or your defined error color).
7. Mini Project: Authentication Screen
Let's assemble a beautiful Login Screen usingConstraintLayout, a hero image, two Outlined TextInputs, and a Material Button.
8. Design Principles
-
Appropriate Input Types: Always set
android:inputType. If asking for an email,textEmailAddressshows the "@" symbol on the keyboard. If asking for a phone number,phoneshows the dialpad.
- Clear Call to Action (CTA): The submit button (e.g., LOGIN) should be the most visually prominent element on the screen.
- Avoid Clutter: Only ask for the information you absolutely need.
9. Common Mistakes
-
Hiding Hints: Using a standard
EditTextwhere the placeholder text disappears once the user types. If they forget what field they are filling, they have to delete their input to see the hint again.TextInputLayoutsolves this.
-
Tiny Touch Targets: Making input fields too short. The Material recommendation is a minimum height of
56dpfor text inputs.
10. Best Practices
- Auto-focus the first input field when the screen opens so the keyboard pops up immediately, saving the user a tap.
- Provide a "Forgot Password?" link positioned logically near the password field or the login button.
11. Exercises
-
1.
Modify the
inputTypeof the email field totextand test it on an emulator. Notice how the keyboard changes. Then change it back totextEmailAddress.
-
2.
Add a "Forgot Password?"
TextViewbelow the Password input layout, aligned to the End (right) of the parent.
12. UI Design Challenges
Challenge: Design a Signup screen that includes: First Name, Last Name, Email, Password, and Confirm Password. Put the First Name and Last Name fields on the same horizontal row usingConstraintLayout (each taking 0dp width and chained together), and the rest stacked vertically below.
13. MCQ Quiz with Answers
What is the primary visual benefit of TextInputLayout over a standard EditText?
Which attribute easily adds an "eye" icon to toggle password visibility?
14. Interview Questions
-
Q: How does setting
android:inputTypeaffect the user experience?
- Q: Explain how you would display a validation error on a specific text field without using a Toast or Dialog.
15. Summary
In this chapter, we conquered form design. We moved away from basic, unstyled inputs and embraced the powerfulTextInputLayout to create robust, accessible, and beautifully styled fields. We learned how to toggle passwords, show clear error states, and combine it all into a professional authentication UI.