CHAPTER 10
Beginner
Creating User Interfaces with XML
Updated: May 16, 2026
25 min read
# CHAPTER 10
Creating User Interfaces with XML
1. Introduction
A brilliantly coded application is useless if the user cannot interact with it. In Native Android development, the logic is written in Kotlin, but the visual aesthetic—the buttons, text fields, colors, and margins—is constructed using XML (eXtensible Markup Language). While modern frameworks like Jetpack Compose are gaining traction, millions of existing enterprise applications (and standard Android tutorials) rely heavily on XML for their UI foundation. In this chapter, we will master Creating User Interfaces with XML. We will explore the structure of XML tags, understand the parent-child relationship of the View Hierarchy, and utilize the Android Studio Visual Layout Editor.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the syntax and structure of XML markup tags.
-
Differentiate between a
Viewand aViewGroup.
- Construct a hierarchical layout architecture.
-
Utilize standard XML attributes (
layoutwidth,layoutheight).
- Navigate the Android Studio Split-Screen Layout Editor.
3. What is XML?
XML is a markup language. It is not a programming language; it does not "think" or calculate logic. It simply describes structural data using Tags. In Android, XML is used to explicitly describe how the screen should look.Syntax Rules:
-
1.
Every element must have an opening tag
<TextView>and a closing tag</TextView>.
-
2.
If an element has no "children" inside it, it can be self-closed:
<TextView />.
- 3. Elements are modified using Attributes placed inside the opening tag.
4. Views and ViewGroups
Everything you see on an Android screen is aView.
-
View: A single UI component (e.g.,TextView,Button,ImageView).
-
ViewGroup: An invisible container that holds multipleViewsand arranges them (e.g., placing them in a vertical column or a horizontal row).LinearLayoutis a standardViewGroup.
*The Hierarchy Rule:* A ViewGroup is the Parent. The Views inside it are the Children.
5. Writing Your First XML Layout
Let's look at the XML required to build a simple screen with a vertical layout holding a piece of text and a button.
xml
6. Critical Attributes (width and height)
EVERY single View in Android MUST have a layoutwidth and a layoutheight. If you forget them, the app will refuse to compile.
You have three options for sizing:
-
1.
wrapcontent: The view shrinks to exactly fit the text/image inside it.
-
2.
matchparent: The view stretches to fill all available space given by its parent container.
-
3.
Hardcoded values: (e.g.,
200dp). *Avoid this unless absolutely necessary, as it ruins responsiveness on different screen sizes!*
7. Understanding DP and SP
Never use "pixels" (px) in Android. A 100px box looks huge on a cheap phone but microscopic on a 4K display.-
dp(Density-independent Pixels): Used for sizing boxes, margins, and images. It scales automatically based on the phone's screen density!
-
sp(Scale-independent Pixels): Used ONLY for Text. It scales based on the screen density AND the user's accessibility font settings in their OS.
8. The Android Studio Layout Editor
You don't have to type all this XML blindly.-
1.
Open any file in
res/layout/.
- 2. In the top right corner of the editing window, look for three icons: Code, Split, and Design.
- 3. Click "Split".
- 4. You will now see your XML code on the left, and a live rendering of the phone screen on the right! As you type XML, the phone screen updates instantly.
9. Mini Project: Simple Profile Screen
Let's combine everything to build a static profile card.
xml
10. Common Mistakes
-
Forgetting to close tags: If you write
<TextView layoutwidth="wrapcontent">but forget to add</TextView>(or the self-closing/>), the entire XML file will turn red with syntax errors.
-
Putting
dpon Text: ApplyingtextSize="24dp"is technically allowed, but it is a massive accessibility violation. If an elderly user increases their phone's font size in the settings,dptext will refuse to grow, rendering your app unusable for them. Always usespfor text.
11. Best Practices
-
Use the
strings.xmlfile: In the examples above, we wroteandroid:text="Welcome!"to make the code easy to read. In a professional app, Android Studio will highlight this in yellow, warning you against "hardcoding strings." You should always define text in theres/values/strings.xmlfile, and reference it viaandroid:text="@string/welcomemessage".
12. Exercises
-
1.
Open Android Studio, navigate to
res/layout/activitymain.xml, and switch to the "Split" view.
-
2.
Add a
<Button>element below the default TextView. Give it a width ofmatchparentand a height ofwrapcontent.
13. Coding Challenges
Challenge: Modify the Profile Screen mini-project. Wrap the twoTextView elements inside an inner LinearLayout with a white background (#FFFFFF), a padding of 16dp, and a margin top of 20dp. This will create the visual effect of a raised "Card" displaying the user's info!
14. MCQ Quiz with Answers
Question 1
In Android UI architecture, what is the critical functional difference between a View and a ViewGroup?
Question 2
When defining the android:textSize attribute for typography, which specific unit of measurement must be utilized to comply with OS-level accessibility scaling settings?
15. Interview Questions
-
Q: Explain the necessity of the
layoutwidthandlayoutheightattributes. Contrast the behavior ofmatchparentversuswrapcontentwhen applied to aButton.
-
Q: Describe the architectural and accessibility benefits of extracting hardcoded string literals from UI XML into the centralized
res/values/strings.xmlfile.
-
Q: Why is the utilization of absolute hardware pixels (
px) strictly forbidden in modern Android layout engineering?
16. FAQs
Q: Can I just drag and drop buttons on the screen instead of typing XML? A: Yes. Android Studio has a "Design" tab where you can physically drag buttons onto a phone canvas. While convenient for absolute beginners, professional developers rarely use it because the auto-generated XML is often bloated and constrained poorly. Typing XML provides absolute pixel-perfect control.17. Summary
In Chapter 10, we established the foundational language of Android visual design. We embraced XML to engineer strict, hierarchical UI trees, nesting individual UI Views within structural ViewGroups. We mastered critical sizing algorithms, dynamically flexing components utilizingmatchparent and wrapcontent. Finally, we ensured our applications scaled flawlessly across diverse hardware utilizing density-independent dp metrics, and respected user accessibility settings by enforcing sp typography standards.