Android Development Android UI and Layouts 3 — Questions and Answers
Question 1: What is the role of the merge tag in Android XML layouts?
- To eliminate redundant view groups when including layouts (Correct answer)
- To merge two activities
- To combine multiple adapters
- To join fragments
Correct answer: To eliminate redundant view groups when including layouts
The <merge> tag helps reduce the depth of the view hierarchy by eliminating a redundant parent layout when using <include>.
Question 2: Which ConstraintLayout feature allows multiple views to be evenly spaced using a chain?
- Chain style (Correct answer)
- Group constraint
- Barrier
- Guideline
Correct answer: Chain style
Chains in ConstraintLayout allow you to control how a group of views distribute space and position relative to each other.
Question 3: What is a ViewHolder pattern used for in Android?
- To cache view references and avoid repeated calls to findViewById (Correct answer)
- To hold the ViewModel instance
- To store Fragment back stack
- To manage lifecycle of views
Correct answer: To cache view references and avoid repeated calls to findViewById
The ViewHolder pattern stores references to views within a list item, eliminating the costly lookup via findViewById on every scroll.
Question 4: Which XML attribute sets a fixed aspect ratio for a view in ConstraintLayout?
- app:layout_constraintDimensionRatio (Correct answer)
- android:ratio
- app:aspectRatio
- android:scaleType
Correct answer: app:layout_constraintDimensionRatio
app:layout_constraintDimensionRatio constrains one dimension of a view relative to the other based on a specified ratio.
Question 5: What method inflates a layout resource in Android?
- LayoutInflater.inflate()
- View.inflate()
- Context.inflate()
- Both A and B (Correct answer)
Correct answer: Both A and B
Both LayoutInflater.inflate() and View.inflate() can be used to instantiate a layout XML file into its corresponding View objects.
Question 6: Which attribute in RecyclerView sets the orientation for LinearLayoutManager?
- app:layoutManager with orientation parameter (Correct answer)
- android:orientation
- app:orientation
- android:scrollDirection
Correct answer: app:layoutManager with orientation parameter
You configure LinearLayoutManager with HORIZONTAL or VERTICAL orientation either in code or via XML with app:layoutManager.
What is the role of the merge tag in Android XML layouts?