Bird
Raised Fist0
Android Kotlinmobile~20 mins

Modifier basics (padding, size, clickable) in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Modifier Basics Screen
A simple screen demonstrating how to use padding, size, and clickable modifiers on a Text composable in Jetpack Compose.
Target UI
-------------------------
|                       |
|   [ Click Me Text ]    |
|                       |
-------------------------
Display a Text composable with the text 'Click Me'.
Add padding of 16.dp around the Text.
Set the size of the Text composable to 200.dp width and 60.dp height.
Make the Text clickable. When clicked, show a Toast message 'Text clicked!'.
Starter Code
Android Kotlin
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      ModifierBasicsScreen()
    }
  }
}

@Composable
fun ModifierBasicsScreen() {
  val context = LocalContext.current
  Box(
    modifier = Modifier
      // TODO: Add padding of 16.dp
      // TODO: Set size to 200.dp width and 60.dp height
      // TODO: Make clickable to show Toast 'Text clicked!'
  ) {
    Text(text = "Click Me")
  }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      ModifierBasicsScreen()
    }
  }
}

@Composable
fun ModifierBasicsScreen() {
  val context = LocalContext.current
  Box(
    modifier = Modifier
      .padding(16.dp)
      .size(width = 200.dp, height = 60.dp)
      .clickable {
        Toast.makeText(context, "Text clicked!", Toast.LENGTH_SHORT).show()
      }
  ) {
    Text(text = "Click Me")
  }
}

We use Modifier.padding(16.dp) to add space around the Text so it doesn't touch the edges.

The Modifier.size(200.dp, 60.dp) sets the width and height of the Box containing the Text.

The Modifier.clickable makes the Box respond to taps. When tapped, it shows a Toast message "Text clicked!" using the current context.

This demonstrates how to chain modifiers to control layout and interaction in Jetpack Compose.

Final Result
Completed Screen
-------------------------
|                       |
|   [ Click Me Text ]    |
|                       |
-------------------------
When user taps on the 'Click Me' text area, a short Toast message 'Text clicked!' appears at the bottom of the screen.
Stretch Goal
Add a background color to the clickable area that changes color when pressed.
💡 Hint
Use Modifier.background with remember { mutableStateOf() } and Modifier.pointerInput or Modifier.indication to detect press state.

Practice

(1/5)
1. What does the padding modifier do in Jetpack Compose?
easy
A. Changes the background color of a UI element
B. Makes a UI element respond to clicks
C. Adds space around the content inside a UI element
D. Sets the width and height of a UI element

Solution

  1. Step 1: Understand the role of padding

    Padding adds empty space inside the element boundary but outside its content.
  2. Step 2: Compare with other modifiers

    Size sets dimensions, clickable adds tap response, background changes color, so padding is about spacing.
  3. Final Answer:

    Adds space around the content inside a UI element -> Option C
  4. Quick Check:

    Padding = space around content [OK]
Hint: Padding always adds space inside element edges [OK]
Common Mistakes:
  • Confusing padding with margin (margin is outside element)
  • Thinking padding changes size directly
  • Mixing padding with clickable behavior
2. Which of the following is the correct syntax to add 16.dp padding to a Text composable?
easy
A. Text("Hello", Modifier.padding(16.dp))
B. Text("Hello", Modifier.padding(16))
C. Text("Hello", Modifier.padding(16.px))
D. Text("Hello", Modifier.padding(16.dp, 16.dp, 16.dp))

Solution

  1. Step 1: Recognize correct unit usage

    In Jetpack Compose, padding requires a Dp value, so 16.dp is correct.
  2. Step 2: Check syntax correctness

    Text("Hello", Modifier.padding(16)) misses the .dp unit, C uses .px which is invalid, D tries to pass three parameters which is not the standard padding call.
  3. Final Answer:

    Text("Hello", Modifier.padding(16.dp)) -> Option A
  4. Quick Check:

    Use .dp for dimension units [OK]
Hint: Use .dp suffix for padding values in Compose [OK]
Common Mistakes:
  • Omitting .dp unit causing type errors
  • Using .px which is not valid in Compose
  • Passing multiple parameters incorrectly
3. What will happen when this code runs?
Box(
  Modifier.size(100.dp).padding(20.dp).clickable { /* do something */ }
) {
  Text("Click me")
}
medium
A. Box is 100x100 dp with 20 dp padding inside and responds to clicks
B. Box is 140x140 dp because padding adds outside size
C. Box is 100x100 dp but padding disables clickable
D. Box ignores padding and clickable modifiers

Solution

  1. Step 1: Understand modifier order and effects

    Size sets the Box to 100x100 dp. Padding adds space inside the Box boundary, reducing content area but not increasing Box size.
  2. Step 2: Check clickable behavior

    Clickable modifier makes the Box respond to taps; it is not disabled by padding.
  3. Final Answer:

    Box is 100x100 dp with 20 dp padding inside and responds to clicks -> Option A
  4. Quick Check:

    Size fixes size, padding adds inner space, clickable enables taps [OK]
Hint: Padding adds space inside size, clickable always works [OK]
Common Mistakes:
  • Thinking padding increases total size
  • Assuming padding disables clickable
  • Ignoring modifier order effects
4. Identify the error in this code snippet:
Text(
  "Tap me",
  Modifier.clickable { println("Clicked") }.padding(10.dp)
)
medium
A. Text composable cannot have clickable modifier
B. Clickable modifier must be last to work properly
C. Missing .dp unit in padding
D. Padding should come before clickable to make padding area clickable

Solution

  1. Step 1: Understand modifier order impact

    Clickable only applies to the area defined by previous modifiers. Padding after clickable means padding area is not clickable.
  2. Step 2: Correct modifier order

    Placing padding before clickable makes the entire padded area respond to clicks.
  3. Final Answer:

    Padding should come before clickable to make padding area clickable -> Option D
  4. Quick Check:

    Modifier order affects clickable area [OK]
Hint: Put padding before clickable to include padding in tap area [OK]
Common Mistakes:
  • Assuming clickable works regardless of modifier order
  • Thinking clickable must be last always
  • Confusing padding units
5. You want a Button with a fixed size of 150x50 dp, 12 dp padding inside, and it should respond to clicks on the entire visible area including padding. Which modifier chain is correct?
hard
A. Modifier.padding(12.dp).size(150.dp, 50.dp).clickable { /* action */ }
B. Modifier.size(150.dp, 50.dp).padding(12.dp).clickable { /* action */ }
C. Modifier.clickable { /* action */ }.size(150.dp, 50.dp).padding(12.dp)
D. Modifier.clickable { /* action */ }.padding(12.dp).size(150.dp, 50.dp)

Solution

  1. Step 1: Ensure size sets total dimensions

    Size should come before padding to fix the total size, with padding inside that size.
  2. Step 2: Place clickable last to cover entire area

    Clickable should be last so it applies to the full size including padding.
  3. Step 3: Analyze options

    Modifier.size(150.dp, 50.dp).padding(12.dp).clickable { /* action */ } sets size first, then padding inside that size, then clickable covers all.
  4. Final Answer:

    Modifier.size(150.dp, 50.dp).padding(12.dp).clickable { /* action */ } -> Option B
  5. Quick Check:

    Size before padding, clickable last for full tap area [OK]
Hint: Put size before padding, clickable last for full clickable area [OK]
Common Mistakes:
  • Putting size after padding causing size to include padding and grow
  • Clickable before padding excludes padding from tap area
  • Confusing order of modifiers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard