tezvyn:

Explain the difference between LinearLayout, RelativeLayout, and FrameLayout

Curated by the Tezvyn teamSource: developer.android.combeginner
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 THIS TESTS: 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.

A GOOD ANSWER COVERS: 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.

COMMON WRONG ANSWERS: 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.

LIKELY FOLLOW-UPS: 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.

ONE 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.

Source: developer.android.com

Read the original → developer.android.com

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.

Explain the difference between LinearLayout, RelativeLayout, and FrameLayout · Tezvyn