Android SharedPreferences Tutorial and Example

Build responsive Android UIs with ConstraintLayout

Android screens rarely arrive in one predictable size. A phone may be compact, a tablet expansive, a foldable change shape mid-session, and a landscape layout leave far less vertical room. Responsive design therefore means preserving hierarchy and usable touch targets while letting the arrangement breathe, rather than shrinking an entire portrait design until it fits.

ConstraintLayout gives that flexibility through relationships between widgets. Instead of calculating pixel positions, you anchor a button to the bottom of its parent, link two text fields, or balance several cards in a row. The same constraints can respond to different widths, orientations and content lengths, which makes the layout easier to maintain than layers of fixed margins.

For Android developers working in XML, the practical approach is to establish a strong root ConstraintLayout, add only the relationships each screen needs, and then test the result with realistic content. This guide covers responsive anchors, resource qualifiers, guidelines, barriers, chains and ratios, with patterns suitable for forms, list items and dialogs.

Replace absolute positions with relationships

Begin by treating the parent as the source of truth. Use wrap_content when a widget should fit its own text or image, and 0dp with opposing start and end constraints when it should occupy the available space. Top and bottom constraints control vertical placement, while margins add intentional space around the constrained edges.

Here, a title spans its parent because both horizontal edges are constrained. The XML belongs inside a ConstraintLayout, whose namespace is declared by the parent tag:

<TextView
    android:id="@+id/screenTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/screen_title"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Prefer this relationship model over absolute layout_margin values or a fixed layout_width. If the label becomes longer or the window narrows, the constraint remains valid; a hard-coded position would not.

Create breakpoints with resource qualifiers

Constraints establish relationships inside the current configuration, while resource qualifiers supply deliberate alternatives. A portrait phone layout can live in res/layout, a tablet version in res/layout-sw600dp, and a landscape variant in res/layout-land. Dimension values can also be qualified, such as res/values-sw600dp, so spacing changes without duplicating every view.

Australia’s Android audience uses a broad mix of devices through Google Play, including compact Samsung phones, tablets and foldables. A conservative design should remain usable near 320 dp, expand around the 600 dp tablet threshold, and avoid assuming a permanent bottom navigation bar. Resource versions should adjust composition and spacing, not invent unrelated screens; shared styles and reusable includes keep the variants aligned.

Position widgets with guidelines and barriers

Guidelines add proportional anchors that adapt to the parent’s dimensions. A horizontal guideline at 35 per cent can divide a reading pane from a control pane, while a vertical guideline can keep a form aligned on a wider screen. Unlike hard-coded margins, percentage guidelines retain their relative relationship as the available space changes.

Barriers solve a different problem: they find the furthest edge of several widgets and position another widget relative to that edge. For example, a form row containing a short label and a long Australian destination such as Mount Gambier can use a barrier so the input starts below both. This handles variable content without a special layout for every possible string.

Balance rows with chains and ratios

Chains arrange widgets in sequence. A packed chain keeps them close together, a spread chain distributes free space between them, and a weighted chain gives each item a share of that space. These modes suit toolbars, rating controls, step indicators and action rows where equal width is not always appropriate.

Layout approach Strongest use Responsive behaviour Common limitation
Fixed dimensions Static demo Poor Clips or leaves gaps
Nested LinearLayouts Simple vertical stacks Fair for one axis Deep hierarchies and awkward cross-axis alignment
ConstraintLayout Forms, lists and adaptive rows Strong across widths and orientations Dense relationships need careful testing

Ratios provide another compact option for paired panels. Constrain one side of the first panel to the other and assign a ratio such as 2:1, then use guidelines to control the pair’s overall footprint. Start with simple relationships and test each edge in the Layout Editor; circular dependencies and accidental over-constraints are common when every possible alignment is expressed at once.

Apply constraints to common app patterns

Forms, list rows and dialogs all benefit from the same principle: anchor content to stable reference points rather than to other fragile coordinates. In a form, place labels above inputs and reserve space for validation text. In a RecyclerView or ListView row, constrain an icon to the start, the title after it, and a chevron to the end.

Useful layout patterns

  • Use one ConstraintLayout as the item root.
  • Connect text with barriers when labels vary.
  • Keep primary actions within thumb reach.
  • Apply ratios only to paired regions.

Because row views are inflated repeatedly, keep their hierarchy shallow and avoid a nested LinearLayout for every alignment. ConstraintLayout is particularly useful here: one item can accommodate a short title or a multi-line value without changing the adapter. For dialog content, constrain the message, progress indicator and action buttons, then check the result with large fonts.

Checks before release

  • Preview at compact and tablet widths.
  • Rotate through supported orientations.
  • Test with maximum-length content.
  • Measure layout and scrolling performance.

Keep the adaptive rules in the layout layer when possible. Moving breakpoints into code may be justified for live navigation or foldable posture, but it increases state handling and should follow a clear device or configuration signal rather than an arbitrary screen-width guess.

Test for accessibility and Australian conditions

Accessibility is part of responsive behaviour, not a final polish step. Give every meaningful icon a content description, keep interactive controls at least 48 dp in both dimensions, support font scaling, and verify colour contrast. Test the screen with TalkBack and keyboard or switch access. Australian Government services must meet the Digital Service Standard, including WCAG 2.1 AA, while commercial apps should use the same benchmark.

Everyday use adds local constraints. Bright sunlight across Sydney, Melbourne and regional Australia can make subtle grey text hard to read, so test contrast outdoors and avoid conveying meaning through colour alone. Public transport is a common phone moment in Melbourne and Sydney: commuters check routes or scan tickets one-handed while moving between platforms, making large targets and clear bottom-of-screen actions especially valuable.

Connectivity also varies beyond metropolitan areas, so loading indicators, cached content and meaningful offline messages should be designed into the layout. If the app collects personal information, organisations covered by the Privacy Act 1988 must follow the Australian Privacy Principles: explain why data is needed, request only relevant permissions and avoid exposing identifying details in the interface. These realities reward layouts that remain legible under poor network conditions and generous text sizes.