Skip to content
tezvyn:

LinearLayout vs. RelativeLayout vs. FrameLayout: Explain and give use cases

Source: developer.android.comEasyHow cards are made

LinearLayout vs. RelativeLayout vs. FrameLayout: Explain and give use cases

Tests your grasp of classic Android layouts and performance trade-offs. A good answer defines each, gives a clear use case, and mentions layout_weight. A red flag is proposing nested LinearLayouts for complex UIs, ignoring performance.

What's really being asked

This question checks your fundamental knowledge of the classic Android View system. It's not just about definitions; it's about your ability to choose the right tool for the job based on UI requirements and performance trade-offs. For a senior candidate, it's also a test of awareness of modern best practices, like preferring ConstraintLayout for complex views to avoid deep, inefficient layout hierarchies.

The full answer

A strong answer explains each layout and provides a distinct, appropriate use case. First, define LinearLayout as arranging views in a single direction, either vertically or horizontally, and mention layout_weight for distributing remaining space. Second, describe RelativeLayout as positioning child views relative to each other or to the parent container. Third, explain FrameLayout as a simple container that stacks children on top of one another, with the last-added child on top. Finally, a senior-level answer briefly mentions that for complex UIs, ConstraintLayout is now preferred over nesting these older layouts to create a flatter, more performant view hierarchy.

The mistakes people make

A major red flag is suggesting a deeply nested LinearLayout structure to build a complex UI. This indicates a lack of awareness of the performance cost, specifically the "double taxation" problem where the layout is measured twice. Another common mistake is being unable to provide a clear, simple use case that uniquely fits one layout type, for example, confusing the stacking of FrameLayout with the relative positioning of RelativeLayout. Forgetting to mention layout_weight when discussing LinearLayout is also a missed opportunity to show deeper knowledge.

What usually comes next

Be prepared for questions like: "What is 'double taxation' and why is it bad for performance?" or "When would you choose ConstraintLayout over all of these?" or "How would you implement a specific UI, like a user profile header, using these layouts versus ConstraintLayout?"

A concrete example

For FrameLayout, imagine an image viewer. The base layer is an ImageView filling the layout. On top of it, you can place a ProgressBar in the center to show loading. Once loaded, the ProgressBar is hidden. You could also have a semi-transparent LinearLayout with action buttons (Share, Delete) at the bottom, which is also a child of the FrameLayout and thus appears stacked on top of the image.

Interview question

Which layout is most appropriate for displaying an image with a loading spinner centered on top and action buttons overlaid at the bottom?

  • a.A deeply nested structure of LinearLayouts
  • b.RelativeLayout, positioning elements relative to the parent and each other
  • c.FrameLayout, as it stacks child views on top of each otherCorrect
  • d.LinearLayout, using layout_weight to distribute space
Why?

FrameLayout is designed for stacking views on top of each other, making it the most direct and efficient choice for overlaying elements like a spinner and buttons on an image. While RelativeLayout could achieve a similar visual effect, FrameLayout is simpler for pure stacking, and deeply nested LinearLayouts are a performance anti-pattern due to 'double taxation'.

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