Back to Course
UI Design

Android UI Design — Layouts, Views & ViewGroups

The user interface (UI) is what your users see and interact with. In Android, UI components are built using a hierarchy of Views and ViewGroups defined in XML layouts. Mastering UI design is essential for creating beautiful, responsive, and user-friendly apps.

Production Reality: Your app's UI is the first thing users notice. A well-designed, responsive UI can make the difference between a successful app and one that users uninstall after the first use. Follow Material Design guidelines for the best results.

1. The Android UI Hierarchy

Every Android UI is built using a tree structure of Views (the leaves) and ViewGroups (the branches).

View Hierarchy (Tree Structure)
┌─────────────────────────────────────────────────────────────┐
│ Root ViewGroup │
│ (e.g., ConstraintLayout) │
└─────────────────────────────────────────────────────────────┘

┌────────────────┴────────────────┐
│ │
▼ ▼
┌──────────────────────┐ ┌──────────────────────┐
│ ViewGroup │ │ View │
│ (LinearLayout) │ │ (TextView) │
└──────────────────────┘ └──────────────────────┘

┌───────┴───────┐
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│ View │ │ View │
│(Button) │ │(EditText)│
└─────────┘ └─────────┘
ComponentDescriptionExamples
ViewA single UI elementTextView, Button, EditText, ImageView
ViewGroupA container that holds Views and other ViewGroupsLinearLayout, RelativeLayout, ConstraintLayout

2. Layouts — The Containers

2.1 LinearLayout

LinearLayout arranges its children in a single row (horizontal) or column (vertical). It's simple and widely used.

<!-- Vertical LinearLayout -->
<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="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />

</LinearLayout>
AttributeDescriptionValues
android:orientationDirection of arrangementvertical or horizontal
android:gravityAlignment of childrencenter, start, end, top, bottom
android:weightSumTotal weight for distribution1, 2, 3, etc.
android:layout_weightProportion of space to occupy1, 2, 3, etc.
<!-- LinearLayout with weight distribution (50/50) -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <View
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#FF5722" />

    <View
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#4CAF50" />

</LinearLayout>

2.2 RelativeLayout

RelativeLayout positions children relative to the parent or to other children. It's flexible but can become complex.

<!-- RelativeLayout with positioning -->
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_marginTop="16dp"
        android:hint="Enter text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit"
        android:layout_below="@id/input"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="16dp" />

</RelativeLayout>
AttributeDescription
android:layout_alignParentTopAlign to the top of parent
android:layout_alignParentBottomAlign to the bottom of parent
android:layout_alignParentStartAlign to the start of parent
android:layout_alignParentEndAlign to the end of parent
android:layout_centerHorizontalCenter horizontally
android:layout_centerVerticalCenter vertically
android:layout_centerInParentCenter in parent
android:layout_belowPosition below another view
android:layout_abovePosition above another view
android:layout_toStartOfPosition to the start of another view
android:layout_toEndOfPosition to the end of another view

2.3 ConstraintLayout — The Modern Standard

ConstraintLayout is the most powerful and flexible layout. It's the recommended layout for modern Android development because it's more efficient than nested layouts.

<!-- ConstraintLayout with constraints -->
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <TextView
        android:id="@+id/helloText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, World!"
        android:textSize="24sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/clickButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        app:layout_constraintTop_toBottomOf="@id/helloText"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp" />

    <EditText
        android:id="@+id/nameInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter your name"
        app:layout_constraintTop_toBottomOf="@id/clickButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
Constraint TypeDescriptionExample
Relative ConstraintConstrain to another viewapp:layout_constraintTop_toBottomOf="@id/otherView"
Parent ConstraintConstrain to parentapp:layout_constraintTop_toTopOf="parent"
BiasAdjust positioning between constraintsapp:layout_constraintHorizontal_bias="0.3"
RatioMaintain aspect ratioapp:layout_constraintDimensionRatio="16:9"
GuidelineInvisible line for positioningapp:layout_constraintGuide_percent="0.5"
Pro Tip: ConstraintLayout is the recommended layout for all new projects. It reduces nesting, improves performance, and provides a visual editor in Android Studio. Avoid deep nesting of layouts (more than 3 levels) as it hurts performance.

3. Common Views (Widgets)

ViewPurposeKey Attributes
TextViewDisplay textandroid:text, android:textSize, android:textColor
EditTextText input fieldandroid:hint, android:inputType
ButtonClickable buttonandroid:text, android:onClick
ImageViewDisplay imagesandroid:src, android:scaleType
CheckBoxBoolean selectionandroid:checked
RadioButtonSingle selection from groupandroid:checked
SwitchToggle switchandroid:checked
ProgressBarLoading indicatorandroid:indeterminate
RecyclerViewDisplay listsCovered in detail in a later chapter

TextView — The Text Display

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello, World!"
    android:textSize="18sp"
    android:textColor="#333333"
    android:textStyle="bold"
    android:fontFamily="sans-serif-medium"
    android:gravity="center"
    android:padding="8dp" />

EditText — Text Input

<EditText
    android:id="@+id/emailInput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter your email"
    android:inputType="textEmailAddress"
    android:maxLines="1"
    android:padding="12dp"
    android:background="@drawable/edittext_bg" />

Button — Clickable Action

<Button
    android:id="@+id/submitButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:textSize="16sp"
    android:backgroundTint="#4CAF50"
    android:textColor="#FFFFFF"
    android:padding="12dp"
    android:elevation="2dp" />

ImageView — Display Images

<ImageView
    android:id="@+id/profileImage"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/profile_photo"
    android:scaleType="centerCrop"
    android:contentDescription="Profile photo" />

4. Common XML Attributes

AttributePurposeExample Values
android:layout_widthWidth of the viewmatch_parent, wrap_content, 100dp
android:layout_heightHeight of the viewmatch_parent, wrap_content, 200dp
android:paddingSpace inside the view16dp, 8dp
android:layout_marginSpace outside the view16dp, 8dp
android:gravityContent alignment inside the viewcenter, start, end, top
android:layout_gravityView alignment in its parentcenter, start, end
android:visibilityVisibility of the viewvisible, invisible, gone
android:backgroundBackground of the view#FFFFFF, @color/primary, @drawable/bg
android:elevationShadow height2dp, 8dp

5. Practical Example — Login Screen

Here's a complete login screen combining multiple views:

<!-- login_activity.xml -->
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="24dp">

    <!-- App Logo -->
    <ImageView
        android:id="@+id/logoImage"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:src="@drawable/ic_logo"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/welcomeText"
        android:layout_marginTop="40dp" />

    <!-- Welcome Text -->
    <TextView
        android:id="@+id/welcomeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome Back!"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintTop_toBottomOf="@id/logoImage"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="24dp" />

    <!-- Email Input -->
    <EditText
        android:id="@+id/emailInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:padding="16dp"
        android:background="@drawable/rounded_edittext"
        app:layout_constraintTop_toBottomOf="@id/welcomeText"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="32dp" />

    <!-- Password Input -->
    <EditText
        android:id="@+id/passwordInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:inputType="textPassword"
        android:padding="16dp"
        android:background="@drawable/rounded_edittext"
        app:layout_constraintTop_toBottomOf="@id/emailInput"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp" />

    <!-- Forgot Password -->
    <TextView
        android:id="@+id/forgotPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Forgot Password?"
        android:textColor="#4CAF50"
        android:textSize="14sp"
        app:layout_constraintTop_toBottomOf="@id/passwordInput"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="8dp" />

    <!-- Login Button -->
    <Button
        android:id="@+id/loginButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textSize="16sp"
        android:backgroundTint="#4CAF50"
        android:textColor="#FFFFFF"
        android:padding="16dp"
        android:elevation="4dp"
        app:layout_constraintTop_toBottomOf="@id/forgotPassword"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="32dp" />

    <!-- Register Link -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Don't have an account? Register"
        android:textSize="14sp"
        app:layout_constraintTop_toBottomOf="@id/loginButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="16dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

6. Best Practices for UI Design

  • ✅ Use ConstraintLayout — It's the most efficient and flexible layout.
  • ✅ Avoid deep nesting — More than 3 levels of nesting hurts performance.
  • ✅ Use dimension resources — Define dimensions in dimens.xml for consistency.
  • ✅ Use color resources — Define colors in colors.xml for theming.
  • ✅ Use string resources — Define text in strings.xml for localization.
  • ✅ Follow Material Design — Use Material Design components and guidelines.
  • ✅ Test on multiple screen sizes — Ensure your UI works on all devices.
  • ✅ Use vector drawables — Use SVG-based icons for all screen densities.
  • ✅ Add content descriptions — For accessibility (screen readers).
  • ✅ Use themes and styles — Avoid repeating attributes across views.

7. Common UI Pitfalls

Common Mistakes to Avoid:
  • Hardcoding strings: Always use strings.xml for text.
  • Hardcoding colors: Always use colors.xml for colors.
  • Using absolute pixel values: Always use dp and sp for dimensions.
  • Nested layouts: Avoid nesting LinearLayouts inside other LinearLayouts.
  • Not handling configuration changes: Test screen rotation and different screen sizes.

8. Production-Ready Checklist

  • ✅ Use ConstraintLayout — For all new layouts.
  • ✅ Use material components — From com.google.android.material.
  • ✅ Use themes — Define a theme in themes.xml for consistent styling.
  • ✅ Use styles — Define reusable styles for common views.
  • ✅ Use dimens.xml — For all dimension values.
  • ✅ Use colors.xml — For all color values.
  • ✅ Use strings.xml — For all text values.
  • ✅ Test on multiple devices — Use AVDs with different screen sizes.
  • ✅ Enable RTL support — For right-to-left languages.
  • ✅ Use VectorDrawable — For all icons.
Key Takeaway: Android UI design is about more than just making things look good — it's about creating a responsive, accessible, and performant user experience. The Layout Editor in Android Studio is a powerful tool — use it to preview your designs in real-time on different screen sizes.

Ready to master Android Development?

Build real-world Android apps with hands-on projects, mentor-led sessions, and placement support.

Explore Course