Skip to content
tezvyn:

Explain the difference between LinearLayout, RelativeLayout, and FrameLayout

Source: developer.android.comEasyHow cards are made

Explain the difference between LinearLayout, RelativeLayout, and FrameLayout

Understanding of ViewGroup measurement and simplicity versus flexibility tradeoffs. LinearLayout for 1D lists, RelativeLayout for complex sibling rules, FrameLayout for single-child or overlap.

What's really being asked

This question probes your understanding of the Android view measurement and layout pipeline, specifically how different ViewGroups arbitrate space and position. A senior candidate should demonstrate that layout choice is a deliberate performance and maintainability decision, not just habit. The interviewer wants to hear you name the measurement strategy, describe the right scenario, and avoid cargo-culting RelativeLayout as the universal answer.

The full answer

First, LinearLayout arranges children in a single direction, either horizontally or vertically, and supports layout_weight to distribute remaining space proportionally. A great answer mentions that nested LinearLayouts trigger multiple measure passes, so a deep hierarchy is a performance risk, but a single LinearLayout is perfect for a simple row of buttons or a vertical form. Second, RelativeLayout positions children using rules relative to the parent or sibling views, such as alignParentTop or below another ID. It excels when views have complex spatial relationships that would otherwise require deep nesting, though it historically performed two measure passes and should be used judiciously on complex screens. Third, FrameLayout is designed to hold a single child or overlapping children, positioning them at the top-left by default with optional gravity. It is the canonical host for Fragment containers and ideal for layered UIs like a full-screen video with a small overlay button. The candidate should explicitly map each layout to a concrete scenario: LinearLayout for a settings list with weighted labels and toggles, RelativeLayout for a detail screen where a title sits above metadata that sits beside an icon, and FrameLayout for a single Fragment slot or a progress spinner over content.

The mistakes people make

A red flag is stating that RelativeLayout is always the best practice and the other two are obsolete. Another red flag is claiming FrameLayout can only hold one child; it can hold many, but they stack. Candidates who say LinearLayout ignores layout_weight or who describe RelativeLayout as the only way to avoid nesting reveal shallow experience. Also, confusing FrameLayout with CardView or suggesting LinearLayout cannot scroll shows a gap in fundamentals.

What usually comes next

The interviewer may ask when ConstraintLayout replaces RelativeLayout, how layout_weight impacts measure performance in LinearLayout, or why FrameLayout is preferred over FragmentContainerView in legacy code. They might also ask you to compare the measure pass count of a nested LinearLayout versus a flat RelativeLayout, or how you would migrate a deeply nested LinearLayout to ConstraintLayout.

A concrete example

Imagine a media player screen. Use FrameLayout as the root to layer a full-screen SurfaceView for video under a semi-transparent control bar and a centered play button. Use RelativeLayout inside a list item if you need a thumbnail aligned to the left with a title to its right and a subtitle below the title. Use LinearLayout with layout_weight for a bottom row of equally spaced action buttons like Share, Download, and Add to Playlist.

Interview question

Which ViewGroup is the most appropriate choice for a media player screen that layers a full-screen video under a semi-transparent control bar and a centered play button?

  • a.LinearLayout with vertical orientation and layout_weight to allocate proportional space to each layer
  • b.FrameLayout, stacking the video, controls, and button as children and using gravity to center the play buttonCorrect
  • c.A deeply nested hierarchy of horizontal and vertical LinearLayouts to keep the views in separate containers
  • d.RelativeLayout using rules such as alignParentBottom and centerInParent to position the controls over the video
Why?

FrameLayout is explicitly designed to stack children at the top-left by default, making it the canonical host for layered UIs like a video with overlays. RelativeLayout can position views relative to the parent, but it is optimized for complex sibling relationships rather than simple stacking, and a nested LinearLayout hierarchy would trigger multiple measure passes while still failing to overlap views cleanly.

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