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.
1. The Android UI Hierarchy
Every Android UI is built using a tree structure of Views (the leaves) and ViewGroups (the branches).
│ Root ViewGroup │
│ (e.g., ConstraintLayout) │
└─────────────────────────────────────────────────────────────┘
│
┌────────────────┴────────────────┐
│ │
▼ ▼
┌──────────────────────┐ ┌──────────────────────┐
│ ViewGroup │ │ View │
│ (LinearLayout) │ │ (TextView) │
└──────────────────────┘ └──────────────────────┘
│
┌───────┴───────┐
│ │
▼ ▼
┌─────────┐ ┌─────────┐
│ View │ │ View │
│(Button) │ │(EditText)│
└─────────┘ └─────────┘
| Component | Description | Examples |
|---|---|---|
| View | A single UI element | TextView, Button, EditText, ImageView |
| ViewGroup | A container that holds Views and other ViewGroups | LinearLayout, 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>
| Attribute | Description | Values |
|---|---|---|
android:orientation | Direction of arrangement | vertical or horizontal |
android:gravity | Alignment of children | center, start, end, top, bottom |
android:weightSum | Total weight for distribution | 1, 2, 3, etc. |
android:layout_weight | Proportion of space to occupy | 1, 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>
| Attribute | Description |
|---|---|
android:layout_alignParentTop | Align to the top of parent |
android:layout_alignParentBottom | Align to the bottom of parent |
android:layout_alignParentStart | Align to the start of parent |
android:layout_alignParentEnd | Align to the end of parent |
android:layout_centerHorizontal | Center horizontally |
android:layout_centerVertical | Center vertically |
android:layout_centerInParent | Center in parent |
android:layout_below | Position below another view |
android:layout_above | Position above another view |
android:layout_toStartOf | Position to the start of another view |
android:layout_toEndOf | Position 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 Type | Description | Example |
|---|---|---|
| Relative Constraint | Constrain to another view | app:layout_constraintTop_toBottomOf="@id/otherView" |
| Parent Constraint | Constrain to parent | app:layout_constraintTop_toTopOf="parent" |
| Bias | Adjust positioning between constraints | app:layout_constraintHorizontal_bias="0.3" |
| Ratio | Maintain aspect ratio | app:layout_constraintDimensionRatio="16:9" |
| Guideline | Invisible line for positioning | app:layout_constraintGuide_percent="0.5" |
3. Common Views (Widgets)
| View | Purpose | Key Attributes |
|---|---|---|
| TextView | Display text | android:text, android:textSize, android:textColor |
| EditText | Text input field | android:hint, android:inputType |
| Button | Clickable button | android:text, android:onClick |
| ImageView | Display images | android:src, android:scaleType |
| CheckBox | Boolean selection | android:checked |
| RadioButton | Single selection from group | android:checked |
| Switch | Toggle switch | android:checked |
| ProgressBar | Loading indicator | android:indeterminate |
| RecyclerView | Display lists | Covered 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
| Attribute | Purpose | Example Values |
|---|---|---|
android:layout_width | Width of the view | match_parent, wrap_content, 100dp |
android:layout_height | Height of the view | match_parent, wrap_content, 200dp |
android:padding | Space inside the view | 16dp, 8dp |
android:layout_margin | Space outside the view | 16dp, 8dp |
android:gravity | Content alignment inside the view | center, start, end, top |
android:layout_gravity | View alignment in its parent | center, start, end |
android:visibility | Visibility of the view | visible, invisible, gone |
android:background | Background of the view | #FFFFFF, @color/primary, @drawable/bg |
android:elevation | Shadow height | 2dp, 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.xmlfor consistency. - ✅ Use color resources — Define colors in
colors.xmlfor theming. - ✅ Use string resources — Define text in
strings.xmlfor 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
- Hardcoding strings: Always use
strings.xmlfor text. - Hardcoding colors: Always use
colors.xmlfor colors. - Using absolute pixel values: Always use
dpandspfor 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.xmlfor 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.
Ready to master Android Development?
Build real-world Android apps with hands-on projects, mentor-led sessions, and placement support.
.png)