Skip to main content
Android Development with Kotlin
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 View and a ViewGroup.
  • 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. 1. Every element must have an opening tag <TextView> and a closing tag </TextView>.
  1. 2. If an element has no "children" inside it, it can be self-closed: <TextView />.
  1. 3. Elements are modified using Attributes placed inside the opening tag.

4. Views and ViewGroups

Everything you see on an Android screen is a View.
  • View: A single UI component (e.g., TextView, Button, ImageView).
  • ViewGroup: An invisible container that holds multiple Views and arranges them (e.g., placing them in a vertical column or a horizontal row). LinearLayout is a standard ViewGroup.

*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
123456789101112131415161718192021222324
<?xml version="1.0" encoding="utf-8"?>
<!-- THE PARENT: A vertical column layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <!-- CHILD 1: Text -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to the App!"
        android:textSize="24sp"
        android:textColor="#000000" />

    <!-- CHILD 2: A Button -->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Click Me" />

</LinearLayout>

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. 1. wrapcontent: The view shrinks to exactly fit the text/image inside it.
  1. 2. matchparent: The view stretches to fill all available space given by its parent container.
  1. 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. 1. Open any file in res/layout/.
  1. 2. In the top right corner of the editing window, look for three icons: Code, Split, and Design.
  1. 3. Click "Split".
  1. 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
12345678910111213141516171819202122232425262728293031
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="32dp"
    android:background="#F0F0F0">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher_round" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Alex Developer"
        android:textSize="28sp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:layout_marginTop="16dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Android Engineer | Kotlin Enthusiast"
        android:textSize="16sp"
        android:textAlignment="center"
        android:textColor="#555555" />

</LinearLayout>

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 dp on Text: Applying textSize="24dp" is technically allowed, but it is a massive accessibility violation. If an elderly user increases their phone's font size in the settings, dp text will refuse to grow, rendering your app unusable for them. Always use sp for text.

11. Best Practices

  • Use the strings.xml file: In the examples above, we wrote android: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 the res/values/strings.xml file, and reference it via android:text="@string/welcomemessage".

12. Exercises

  1. 1. Open Android Studio, navigate to res/layout/activitymain.xml, and switch to the "Split" view.
  1. 2. Add a <Button> element below the default TextView. Give it a width of matchparent and a height of wrapcontent.

13. Coding Challenges

Challenge: Modify the Profile Screen mini-project. Wrap the two TextView 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 layoutwidth and layoutheight attributes. Contrast the behavior of matchparent versus wrapcontent when applied to a Button.
  • Q: Describe the architectural and accessibility benefits of extracting hardcoded string literals from UI XML into the centralized res/values/strings.xml file.
  • 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 utilizing matchparent 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.

18. Next Chapter Recommendation

We know how to size a box. But what exactly goes inside the box? Proceed to Chapter 11: TextView, Buttons, Images, and Input Fields to master the core interactive components of the Android visual toolkit.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·