TextView, Buttons, and Input Fields
# TextView, Buttons, and Input Fields
1. Introduction
At the core of almost every mobile application is the interaction between text display, user input, and call-to-action buttons. In Android, these are managed byTextView, EditText, and Button (or MaterialButton). In this chapter, we will learn how to go beyond the default grey styles and customize these fundamental widgets to create professional, interactive UI components.
2. Learning Objectives
By the end of this chapter, you will:-
Style text typography using
TextViewattributes.
-
Implement and customize modern
MaterialButtonstyles (Filled, Outlined, Text).
-
Create robust user input forms using
TextInputLayoutandTextInputEditText.
- Handle hints, input types, and basic UI validation states.
- Connect Kotlin code to listen to button clicks.
3. Text Styling (TextView)
TheTextView is used to display non-editable text.
Key attributes for styling:
-
android:textSize: Always usesp(Scale-independent Pixels) (e.g.,16sp).
-
android:textColor: Can reference colors incolors.xmlor use hex codes.
-
android:textStyle:normal,bold, oritalic.
-
android:fontFamily: Apply custom fonts (e.g.,sans-serif-medium).
-
android:lineSpacingExtra: Adds space between lines of text for readability.
4. Material Buttons
Modern Android apps useMaterialButton rather than the legacy Button class. Material Buttons automatically support rounded corners, ripples, and elevation.
Material Design defines three main button hierarchies:
- 1. Filled/Contained Button: High emphasis (Primary action like "Save" or "Buy").
- 2. Outlined Button: Medium emphasis (Secondary action like "Cancel").
- 3. Text Button: Low emphasis (Tertiary action like "Learn More").
You achieve these using styles:
5. Input Fields (EditText & TextInputLayout)
To accept user input, you use anEditText. However, for a modern UI with floating labels, error messages, and rounded borders, you should wrap your TextInputEditText inside a TextInputLayout.
Crucial Attributes:
-
android:hint: The placeholder text that floats to the top when the user starts typing.
-
android:inputType: Changes the keyboard layout (e.g.,textPasswordhides characters,numbershows a number pad).
6. Real-world Design Use Cases
- Checkout Screen: The "Place Order" button must be a thick, brightly colored Filled Button, while the "Continue Shopping" button underneath is an Outlined Button to avoid competing for the user's attention.
-
Login Form: Uses
TextInputLayoutwith thetextPasswordinput type, often enabling theapp:passwordToggleEnabled="true"attribute to let users show/hide their password with an eye icon.
7. Interactive Kotlin Code Example
UI design isn't just about looks; it's about interaction. Here is how you connect your Button to Kotlin code:8. Design Principles
- Visual Hierarchy: Your screen should only have ONE primary (filled) button. Multiple primary buttons confuse the user on what action to take next.
- Clear Labels: Input fields should always have a visible label or hint, even after the user starts typing.
9. Common Mistakes
-
Using
dpfor text size: Text must always usespso it respects the user's device accessibility settings for font size.
-
Raw EditText: Using a plain
<EditText>instead of<TextInputLayout>. A plain EditText lacks modern UI context, state styling, and floating hints.
10. Best Practices
-
Use
inputTypediligently. Showing a full QWERTY keyboard when the user is supposed to enter a phone number is terrible UX.
-
Ensure buttons have a minimum height of
48dpto serve as an easily tappable touch target.
11. Exercises
-
1.
Create an
OutlinedButtonand change its stroke color and border width usingapp:strokeColorandapp:strokeWidth.
-
2.
Create a password input field using
TextInputLayoutand add the toggle icon so the user can see their password.
12. UI Design Challenges
Challenge: Contact Form Design a minimal Contact Form UI containing:- 1. A bold title (TextView).
- 2. A Name input (Outlined).
- 3. A Phone Number input (Outlined, showing number keyboard).
- 4. A multi-line Message input (Outlined, taking up more vertical space).
- 5. A primary "Send Message" button spanning the full width of the screen.
13. MCQ Quiz with Answers
Q1: What unit of measurement MUST be used for the android:textSize attribute?
A) dp
B) px
C) pt
D) sp
*Answer: D*
Q2: In Material Design, what type of button is used for the most important action on the screen? A) Outlined Button B) Filled / Contained Button C) Text Button D) Image Button *Answer: B*
Q3: To get a floating label effect for user input, what component should wrap your EditText? A) FloatingLayout B) LinearLayout C) TextInputLayout D) FormLayout *Answer: C*
14. Interview Questions
-
Why is
TextInputLayoutpreferred over a standardEditTextin modern Android UI?
-
*Answer:*
TextInputLayoutis part of Material Components. It provides built-in support for floating hints, error message rendering below the line, character counting, password toggles, and box/outline styling without needing custom background drawables.
-
Explain the importance of the
android:inputTypeattribute in UX design.
-
*Answer:*
inputTypeinforms the OS which virtual keyboard to display. Showing a number pad for a ZIP code or an email keyboard (with the '@' symbol easily accessible) drastically reduces friction and improves the User Experience.
15. FAQs
Can I change the font of my TextView to a custom downloaded font? Yes. You can place.ttf or .otf font files in the res/font/ directory and apply them using android:fontFamily="@font/my_custom_font".
How do I make a button corners perfectly round (pill shape)?
Set app:cornerRadius to a very high value (e.g., 50dp), or use app:shapeAppearanceOverlay for more complex shape manipulation.
16. Summary
In this chapter, we covered the critical components of text and interaction. We learned how to style text typography correctly usingsp. We explored the Material Design button hierarchy (Filled, Outlined, Text). Finally, we designed modern user input fields using TextInputLayout to create a robust user experience.
17. Next Chapter Recommendation
With text and buttons mastered, you need to know how to add visual flair. In the next chapter, Working with Images, Icons, and Cards, we will coverImageView, vector graphics, and how to use MaterialCardView to add beautiful elevation and shadows to your designs.