Bird
Raised Fist0
Android Kotlinmobile~20 mins

Material Theme customization in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: CustomThemeScreen
This screen demonstrates how to customize the Material Theme colors and typography in an Android app using Kotlin. It shows a simple UI with a title, a button, and a text field styled with the custom theme.
Target UI
-------------------------
| Custom Theme Screen   |
|-----------------------|
| Title (Large Text)    |
|                       |
| [ Text Input Field ]  |
|                       |
| [ Custom Button ]     |
|                       |
-------------------------
Create a custom Material Theme with a primary color of teal_700 and secondary color of amber_500.
Customize typography to use a larger font size for the title text.
Apply the custom theme to the screen.
Add a TextField with hint text 'Enter your name'.
Add a Button labeled 'Submit' styled with the primary color.
Starter Code
Android Kotlin
package com.example.materialthemecustomization

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

class CustomThemeScreen : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            // TODO: Apply custom Material Theme here
            Surface(modifier = Modifier.padding(16.dp)) {
                CustomThemeContent()
            }
        }
    }
}

@Composable
fun CustomThemeContent() {
    var name by remember { mutableStateOf("") }
    Column {
        Text(text = "Title", style = MaterialTheme.typography.headlineMedium)
        // TODO: Add TextField with hint
        // TODO: Add Button labeled 'Submit'
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.materialthemecustomization

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

private val CustomLightColors = lightColorScheme(
    primary = Color(0xFF00796B), // teal_700
    secondary = Color(0xFFFFC107) // amber_500
)

private val CustomTypography = androidx.compose.material3.Typography(
    headlineMedium = TextStyle(
        fontSize = 28.sp,
        fontWeight = FontWeight.Bold
    )
)

class CustomThemeScreen : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MaterialTheme(
                colorScheme = CustomLightColors,
                typography = CustomTypography
            ) {
                Surface(modifier = Modifier.padding(16.dp)) {
                    CustomThemeContent()
                }
            }
        }
    }
}

@Composable
fun CustomThemeContent() {
    var name by remember { mutableStateOf("") }
    Column {
        Text(text = "Title", style = MaterialTheme.typography.headlineMedium)
        TextField(
            value = name,
            onValueChange = { name = it },
            placeholder = { Text(text = "Enter your name") },
            modifier = Modifier.padding(top = 16.dp, bottom = 16.dp)
        )
        Button(onClick = { /* Handle submit */ }) {
            Text(text = "Submit")
        }
    }
}

We created a custom color scheme with teal_700 as primary and amber_500 as secondary colors using lightColorScheme. We customized the typography by increasing the font size and weight of the headlineMedium style for the title text. The MaterialTheme composable applies these custom colors and typography to all child components. The TextField uses a placeholder to show the hint text. The Button uses the primary color automatically from the theme. This setup shows how to customize and apply a Material Theme in a simple screen.

Final Result
Completed Screen
-------------------------
| Custom Theme Screen   |
|-----------------------|
| Title                 |
|                       |
| [ Enter your name    ]|
|                       |
| [ Submit ]            |
|                       |
-------------------------
User can tap the text field and type their name.
User can tap the Submit button (currently no action).
Stretch Goal
Add dark mode support to the custom Material Theme.
💡 Hint
Define a darkColorScheme with appropriate colors and switch themes based on system setting using isSystemInDarkTheme() in Compose.

Practice

(1/5)
1. What is the main purpose of customizing the MaterialTheme in an Android app?
easy
A. To add new activities to the app
B. To change the app's colors, fonts, and shapes globally
C. To manage app permissions
D. To handle network requests

Solution

  1. Step 1: Understand MaterialTheme role

    The MaterialTheme controls the look and feel of the app by setting colors, typography, and shapes.
  2. Step 2: Identify customization purpose

    Customizing MaterialTheme changes these visual styles globally for consistency.
  3. Final Answer:

    To change the app's colors, fonts, and shapes globally -> Option B
  4. Quick Check:

    MaterialTheme customization = change colors, fonts, shapes [OK]
Hint: MaterialTheme controls app-wide style settings [OK]
Common Mistakes:
  • Confusing theme customization with adding app features
  • Thinking it manages app permissions
  • Assuming it handles network or data logic
2. Which of the following is the correct way to apply a custom color scheme in MaterialTheme in Kotlin?
easy
A. MaterialTheme(colorScheme = myColors) { /* UI content */ }
B. MaterialTheme(colors = myColors) { /* UI content */ }
C. MaterialTheme(setColors = myColors) { /* UI content */ }
D. MaterialTheme(applyColors = myColors) { /* UI content */ }

Solution

  1. Step 1: Recall MaterialTheme parameters

    The correct parameter for colors in MaterialTheme is colorScheme.
  2. Step 2: Match correct syntax

    MaterialTheme(colorScheme = myColors) { /* UI content */ } uses colorScheme = myColors, which is the correct syntax in Kotlin.
  3. Final Answer:

    MaterialTheme(colorScheme = myColors) { /* UI content */ } -> Option A
  4. Quick Check:

    Use colorScheme parameter for colors [OK]
Hint: Use colorScheme, not colors, in MaterialTheme [OK]
Common Mistakes:
  • Using 'colors' instead of 'colorScheme'
  • Using non-existent parameters like 'setColors' or 'applyColors'
  • Forgetting to wrap UI content inside the theme block
3. Given the code snippet below, what color will the Text component display if primary in colorScheme is set to Color.Red?
MaterialTheme(
  colorScheme = ColorScheme(
    primary = Color.Red,
    secondary = Color.Blue
  )
) {
  Text("Hello", color = MaterialTheme.colorScheme.primary)
}
medium
A. Blue
B. Green
C. Red
D. Default text color

Solution

  1. Step 1: Identify colorScheme primary value

    The primary color in colorScheme is set to Color.Red.
  2. Step 2: Check Text color usage

    The Text color uses MaterialTheme.colorScheme.primary, so it will be Color.Red.
  3. Final Answer:

    Red -> Option C
  4. Quick Check:

    Text color = primary color = Red [OK]
Hint: Text color uses primary from colorScheme [OK]
Common Mistakes:
  • Confusing primary with secondary color
  • Assuming default color instead of theme color
  • Mixing up color names
4. What is wrong with the following Kotlin code that tries to customize the MaterialTheme's typography?
MaterialTheme(
  typography = Typography(
    bodyLarge = TextStyle(fontSize = 20)
  )
) {
  Text("Welcome")
}
medium
A. TextStyle requires a unit for fontSize, like sp or dp
B. Typography cannot be customized in MaterialTheme
C. Text component must specify typography explicitly
D. MaterialTheme must not have a typography parameter

Solution

  1. Step 1: Check TextStyle fontSize usage

    The fontSize must include a unit like 20.sp, not just 20.
  2. Step 2: Identify correct syntax

    Without the unit, the code will cause a type error because fontSize expects a TextUnit.
  3. Final Answer:

    TextStyle requires a unit for fontSize, like sp or dp -> Option A
  4. Quick Check:

    fontSize needs unit like sp [OK]
Hint: Always add .sp for fontSize values [OK]
Common Mistakes:
  • Omitting units for fontSize
  • Thinking typography can't be customized
  • Expecting Text to need explicit typography parameter
5. You want to create a custom shape for buttons in your app using MaterialTheme. Which approach correctly applies a rounded corner shape of 16.dp to all buttons?
hard
A. MaterialTheme(shapes = Shapes(extraLarge = RoundedCornerShape(16.dp))) { Button(...) }
B. MaterialTheme(shapes = Shapes(small = RoundedCornerShape(16.dp))) { Button(...) }
C. MaterialTheme(shapes = Shapes(large = RoundedCornerShape(16.dp))) { Button(...) }
D. MaterialTheme(shapes = Shapes(medium = RoundedCornerShape(16.dp))) { Button(...) }

Solution

  1. Step 1: Understand shape categories in MaterialTheme

    MaterialTheme defines shapes as small, medium, and large. Buttons use the medium shape by default.
  2. Step 2: Apply rounded corners to medium shape

    Setting medium = RoundedCornerShape(16.dp) customizes button corners.
  3. Final Answer:

    MaterialTheme(shapes = Shapes(medium = RoundedCornerShape(16.dp))) { Button(...) } -> Option D
  4. Quick Check:

    Buttons use medium shape slot [OK]
Hint: Buttons use medium shape in MaterialTheme [OK]
Common Mistakes:
  • Changing small or large shapes instead of medium
  • Using non-existent shape categories like extraLarge
  • Forgetting to wrap Button inside MaterialTheme
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