Custom UI Components and Reusable Design
# CHAPTER 17
Custom UI Components and Reusable Design
1. Introduction
Imagine building an app with 20 screens. If you manually code the exact same "Submit" button with its specific margins, paddings, rounded corners, and text style 20 different times, you have created a maintenance nightmare. What if the client wants to change the corner radius? You would have to update 20 files! The solution is building Reusable Custom Components. In this chapter, we will learn how to build Compound Views and implement a true Design System in Android.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the DRY (Don't Repeat Yourself) principle in UI design.
-
Use the
<include>tag to reuse XML layouts.
- Build a Custom Compound View using Kotlin and XML.
- Define custom XML attributes (declare-styleable).
- Understand the basics of a Design System.
3. Reusing Layouts with <include>
The simplest way to reuse UI is the <include> tag. If you have a custom Toolbar or a specific Card design that appears on multiple screens, define it once in a separate file.
Step 1: Create layout/mycustomcard.xml:
Step 2: Use it anywhere:
*Tip: You can override android:id, layoutwidth, and layoutheight directly in the <include> tag.*
4. Custom Compound Views
<include> is great for static layouts, but what if you want a custom component that has complex logic attached to it? A Compound View is a custom Kotlin class that inflates a layout and handles its own logic.
Let's build a ProfileInfoRow (an icon on the left, title text, and subtitle text).
Step 1: The XML (viewprofilerow.xml)
*Note: We use <merge> instead of a Layout to prevent unnecessary nesting when we inflate it.*
Step 2: The Kotlin Class
Step 3: Using it in XML
5. Design Systems
A Design System is a collection of reusable components, guided by clear standards, that can be assembled together to build any number of applications. By creating Compound Views for your Buttons, Inputs, Cards, and Headers, you are effectively building your own Design System on top of Android's framework. This ensures that a button looks exactly the same on the Settings screen as it does on the Checkout screen.6. Defining Custom XML Attributes
To make your Compound View truly reusable, you can pass parameters to it directly from XML. Createres/values/attrs.xml:
Then, in your Kotlin init block, you read these attributes using context.obtainStyledAttributes and apply them immediately!
7. Common Mistakes
-
Not using
<merge>: If your custom Kotlin class extendsLinearLayout, and you inflate an XML file that has aLinearLayoutroot, you end up with a LinearLayout inside a LinearLayout. Using<merge>as the root of the inflated XML prevents this.
-
Memory Leaks: If your custom view registers listeners to singletons or static variables, remember to unregister them in
onDetachedFromWindow.
8. Best Practices
- Start small. Don't build a massive custom view framework from day one. When you notice you are copying and pasting the same XML for a 3rd time, extract it into a custom component.
-
Use Android's
styles.xmlto globally style standard widgets before resorting to custom Kotlin classes.
9. Exercises
-
1.
Extract a complex button layout you made in a previous chapter into its own XML file and use the
<include>tag to place it on a different screen.
-
2.
In the
ProfileInfoRowexample, add a functionhideSubtitle()that sets the subtitle's visibility toGONE.
10. UI Design Challenges
Challenge: Create a custom Compound View calledQuantitySelector. It should consist of a "Minus" button, a "Number" TextView, and a "Plus" button. Handle the click logic internally within the Kotlin class so that clicking Plus increases the number, and Minus decreases it (but not below 0).
11. MCQ Quiz with Answers
What is the primary benefit of creating Custom Compound Views?
Which XML tag should you use as the root of a layout being inflated by a custom ViewGroup class to avoid unnecessary layout nesting?
12. Interview Questions
-
Q: Explain how you would pass a custom XML attribute (like
app:customColor) into a custom Kotlin View.
-
Q: What is the difference between styling a standard
Buttonviastyles.xmland creating a customCompound Viewbutton?
13. FAQs
Q: With Jetpack Compose (Kotlin UI), are Custom Views obsolete? A: In the new Jetpack Compose paradigm, building custom components is significantly easier (it's just a Kotlin function). However, millions of apps still use XML, and understanding Compound Views is essential for maintaining or integrating with legacy codebases.14. Summary
Reusability is the hallmark of a professional developer. By leveraging<include>, styles, and Custom Compound Views, we can build a robust Design System. This not only makes our app visually consistent but dramatically reduces the time it takes to build new features.