0
0
Android Kotlinmobile~20 mins

Arrangement and alignment in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Arrangement and Alignment
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)
AText is aligned to the right inside the TextView
BText is aligned to the left inside the TextView
CText is centered horizontally inside the TextView
DText is aligned to the top inside the TextView
Attempts:
2 left
💡 Hint
Gravity.END aligns content to the right side in left-to-right layouts.
ui_behavior
intermediate
2: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>
AButton 1 takes 1/3 and Button 2 takes 2/3 of the horizontal space
BButton 1 takes 2/3 and Button 2 takes 1/3 of the horizontal space
CButtons ignore weight and size themselves to content
DBoth buttons take equal horizontal space
Attempts:
2 left
💡 Hint
Weights divide space proportionally based on their values.
lifecycle
advanced
2: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>
AButtons are packed together at the start with no space between
BButtons overlap each other in the center
CButtons are packed together at the center with no space between
DButtons are spread evenly with equal space between them and edges
Attempts:
2 left
💡 Hint
spread chain style places equal gaps between buttons and edges.
navigation
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>
AImageView is aligned to the top-left corner
BImageView is centered horizontally and aligned to the bottom of the FrameLayout
CImageView is stretched to fill the FrameLayout
DImageView is centered vertically and aligned to the right
Attempts:
2 left
💡 Hint
layout_gravity controls the child's position inside the parent FrameLayout.
📝 Syntax
expert
3: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)
A
constraintSet.setHorizontalBias(R.id.button, 0.5f)
constraintSet.setVerticalBias(R.id.button, 0.5f)
B
constraintSet.centerHorizontally(R.id.button, ConstraintSet.PARENT_ID)
constraintSet.centerVertically(R.id.button, ConstraintSet.PARENT_ID)
C
constraintSet.connect(R.id.button, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
constraintSet.connect(R.id.button, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)
constraintSet.connect(R.id.button, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
constraintSet.connect(R.id.button, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)
D
constraintSet.connect(R.id.button, ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT)
constraintSet.connect(R.id.button, ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT)
Attempts:
2 left
💡 Hint
Centering requires connecting all four sides to parent edges.