Challenge - 5 Problems
Master of Arrangement and Alignment
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Effect of Gravity on View Alignment
Given a LinearLayout with vertical orientation and a child TextView with gravity set to Gravity.END, where will the text appear inside the TextView?
Android Kotlin
val layout = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
}
val textView = TextView(this).apply {
text = "Hello"
gravity = Gravity.END
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
}
layout.addView(textView)Attempts:
2 left
💡 Hint
Gravity.END aligns content to the right side in left-to-right layouts.
✗ Incorrect
The gravity property inside a TextView controls the alignment of the text inside the view's bounds. Gravity.END aligns the text to the right side horizontally.
❓ ui_behavior
intermediate2:00remaining
LinearLayout Weight Distribution
In a horizontal LinearLayout with two Buttons, if the first Button has layout_weight=1 and the second Button has layout_weight=2, how will the available horizontal space be divided?
Android Kotlin
<LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button 1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:text="Button 2" /> </LinearLayout>
Attempts:
2 left
💡 Hint
Weights divide space proportionally based on their values.
✗ Incorrect
The total weight is 3 (1+2). Button 1 gets 1/3 of the space, Button 2 gets 2/3.
❓ lifecycle
advanced2:30remaining
ConstraintLayout Chain Behavior
In a ConstraintLayout, when three buttons are chained horizontally with chainStyle set to spread, how are the buttons arranged?
Android Kotlin
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@+id/button2" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toEndOf="@+id/button1" app:layout_constraintEnd_toStartOf="@+id/button3" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintStart_toEndOf="@+id/button2" app:layout_constraintEnd_toEndOf="parent" /> <!-- Chain style set in code or XML --> </androidx.constraintlayout.widget.ConstraintLayout>
Attempts:
2 left
💡 Hint
spread chain style places equal gaps between buttons and edges.
✗ Incorrect
The spread style distributes buttons evenly with equal space between them and the parent edges.
advanced
2:00remaining
Effect of Layout Gravity in FrameLayout
Inside a FrameLayout, a child ImageView has layout_gravity set to bottom|center_horizontal. Where will the ImageView appear?
Android Kotlin
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:src="@drawable/sample" /> </FrameLayout>
Attempts:
2 left
💡 Hint
layout_gravity controls the child's position inside the parent FrameLayout.
✗ Incorrect
The layout_gravity attribute positions the child inside the FrameLayout. bottom|center_horizontal means horizontally centered and aligned to the bottom edge.
📝 Syntax
expert3:00remaining
Correct ConstraintSet Code for Centering a View
Which Kotlin code snippet correctly centers a Button horizontally and vertically inside a ConstraintLayout using ConstraintSet?
Android Kotlin
val constraintSet = ConstraintSet() constraintSet.clone(constraintLayout) // Missing code here constraintSet.applyTo(constraintLayout)
Attempts:
2 left
💡 Hint
Centering requires connecting all four sides to parent edges.
✗ Incorrect
To center a view in ConstraintLayout using ConstraintSet, you connect all four sides of the view to the corresponding sides of the parent.