Skip to content
tezvyn:

LinearLayout vs. RelativeLayout vs. FrameLayout

Source: developer.android.comEasyHow cards are made

LinearLayout vs. RelativeLayout vs. FrameLayout

This tests your knowledge of fundamental View layouts and performance trade-offs. Define LinearLayout (single axis), RelativeLayout (relative positioning), and FrameLayout (stacking), giving a clear use case for each.

What's really being asked

This question tests your understanding of the foundational building blocks of the Android View system. An interviewer isn't just looking for definitions; they are assessing your ability to choose the right tool for the job, your awareness of layout performance (e.g., the cost of nested layouts and multiple measurement passes), and whether your knowledge is current (i.e., you know when to use ConstraintLayout instead).

The full answer

First, define each layout by its core function. LinearLayout arranges children in a single direction, either vertically or horizontally. RelativeLayout positions children relative to the parent or to other sibling views. FrameLayout stacks children on top of each other, like a deck of cards.

Second, provide a distinct, appropriate use case for each. For LinearLayout, a simple vertical form or a horizontal button bar. For FrameLayout, an image with a centered play icon overlay or a container to host a single Fragment. For RelativeLayout, a user profile header where a profile picture is aligned left, user text is to the right of the picture, and a button is aligned to the parent's far right.

Third, a senior-level answer must mention ConstraintLayout. Acknowledge that for any complex UI that might have required a nested LinearLayout or a RelativeLayout, ConstraintLayout is now the preferred, more performant, and more flexible solution. It helps create flat view hierarchies, avoiding the performance cost of multiple measurement passes common in complex RelativeLayouts or nested LinearLayouts with weights.

The mistakes people make

Failing to mention ConstraintLayout is the biggest red flag for a senior role, as it suggests outdated knowledge. Another common mistake is advocating for deeply nested LinearLayouts to create a complex UI, which is a major performance anti-pattern. Candidates also get flagged for giving poor examples, like using a RelativeLayout for a simple vertical list where a LinearLayout would be cheaper and simpler. Finally, simply mixing up the definitions, especially between RelativeLayout and FrameLayout, shows a weak grasp of the fundamentals.

What usually comes next

Expect follow-ups like: "When exactly is ConstraintLayout better than RelativeLayout?" (Answer: Almost always for new complex layouts due to a more efficient solver and better design-time tooling). Or, "How does using layout_weight in a nested LinearLayout affect performance?" (Answer: It can cause a double measurement pass, slowing down rendering).

A concrete example

To build a user profile header, you'd use a RelativeLayout. The ImageView for the avatar would be aligned to the parent's left. A TextView for the username would be positioned to the right of the avatar. Another TextView for the user handle would be below the username. A 'Follow' Button would be aligned to the parent's right edge. Trying to build this with nested LinearLayouts would be inefficient and complex, while a FrameLayout couldn't position items side-by-side.

Interview question

To place a play button icon directly on top of a video thumbnail image, which layout is the most direct and idiomatic choice to contain both views?

  • a.LinearLayout, using layout_gravity to position the button over the image.
  • b.ConstraintLayout, as it is the most flexible layout for all positioning tasks.
  • c.FrameLayout, as it is designed to stack child views on top of each other.Correct
  • d.RelativeLayout, by aligning the button's edges to the parent container's edges.
Why?

FrameLayout is the ideal choice as its primary purpose is to stack views, making it perfect for overlays like an icon on an image. While RelativeLayout or ConstraintLayout could also work, FrameLayout is the simplest and most semantic tool for this specific job.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles