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 THIS TESTS: 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).
A GOOD ANSWER COVERS: 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.
COMMON WRONG ANSWERS: 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.
LIKELY FOLLOW-UPS: 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).
ONE 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.
Source: developer.android.com
Read the original → developer.android.com
- #android
- #ui
- #layout
- #views
- #performance
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.