Table of Contents

Pathway 3: Interact with UI and state

https://developer.android.com/courses/pathways/android-basics-compose-unit-2-pathway-3

This is a part of Android Basics with Compose course.

1. Understanding state in Compose

Optional video.

var input by remember {
  mutableStateOf("")
}

2. Intro to tip calculator

… Tip = Gratuity (Trinkgeld in DE)

3. Intro to state in Compose

3.1 Before you begin

…

3.2 Get started

…

3.3 Get Starter Code

3.4 Use state in Compose

…

3.5 The Composition

3.6 Use remember function to save state

…

@Composable
fun EditNumberField(modifier: Modifier = Modifier) {
    var amountInput by remember { mutableStateOf("") }
 
 
    TextField(
        value = amountInput,
        onValueChange = { amountInput = it },
        modifier = modifier
    )
}

…

3.7 State and recomposition in action

…

3.8 Modify the appearance

@Composable
fun EditNumberField(modifier: Modifier = Modifier) {
    var amountInput by remember { mutableStateOf("") }
    TextField(
        value = amountInput,
        onValueChange = { amountInput = it },
        singleLine = true,
        label = { Text(stringResource(R.string.bill_amount)) },
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
        modifier = modifier
    )
}

3.9 Display the tip amount

3.10 State hoisting

@Composable
fun TipTimeLayout() {
   var amountInput by remember { mutableStateOf("") }
   val amount = amountInput.toDoubleOrNull() ?: 0.0
   val tip = calculateTip(amount)
 
   Column(
       modifier = Modifier
            .statusBarsPadding()
            .padding(horizontal = 40.dp)
            .verticalScroll(rememberScrollState())
            .safeDrawingPadding(),
       horizontalAlignment = Alignment.CenterHorizontally,
       verticalArrangement = Arrangement.Center
   ) {
       Text(
           text = stringResource(R.string.calculate_tip),
           modifier = Modifier
               .padding(bottom = 16.dp, top = 40.dp)
               .align(alignment = Alignment.Start)
       )
       EditNumberField(
           value = amountInput,
           onValueChange = { amountInput = it },
           modifier = Modifier
               .padding(bottom = 32.dp)
               .fillMaxWidth()
       )
       Text(
           text = stringResource(R.string.tip_amount, tip),
           style = MaterialTheme.typography.displaySmall
       )
       Spacer(modifier = Modifier.height(150.dp))
   }
}
 
@Composable
fun EditNumberField(
   value: String,
   onValueChange: (String) -> Unit,
   modifier: Modifier = Modifier
) {
   TextField(
       value = value,
       onValueChange = onValueChange,
       singleLine = true,
       label = { Text(stringResource(R.string.bill_amount)) },
       keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
       modifier = modifier
   )
}

3.11 Get the solution code

…

3.12 Conclusion

4. Calculate a custom tip

4.1. Before you begin

…

4.2 Get the starter code

…

4.3 Starter app overview

…

4.4 Add a tip-percentage text field

…

…

…

4.5 Set an action button

…

    keyboardOptions = KeyboardOptions.Default.copy(
        keyboardType = KeyboardType.Number,
        imeAction = ImeAction.Next
    ),

4.5 Add a switch

…

Switch(
    checked = roundUp,
    onCheckedChange = onRoundUpChanged,
    modifier = modifier
                .fillMaxWidth()
                .wrapContentWidth(Alignment.End),
    )

…

4.7 Add support for landscape orientation

…

@Composable
fun TipTimeLayout() {
    // ...
    Column(
        modifier = Modifier
            .padding(40.dp)
            .verticalScroll(rememberScrollState()),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        //...
    }
}

4.8 Add leading icon to text fields (optional)

…

TextField(
    value = value,
    leadingIcon = { Icon(painter = painterResource(id = leadingIcon), null) },
    //...
)

4.9 Get the solution code

…

4.10 Conclusion

…

5. Write automated tests

5.1 Before you begin

…

5.2 Get the starter code

…

5.3 Automated tests

…

…

5.4 Write a local test

Local tests directly test methods from the app code, so the methods to be tested must be available to the testing classes and methods. So:

The following list of concepts apply to local and instrumentation tests:

…

5.5 Write an instrumentation test

…

5.6 Get the solution code

…

5.7 Conclusion

Summary:

6. Project: Create an Art Space app

6.1 Before you begin

…

6.2 Build static UI with composables

…

6.3 Make the app interactive

…

6.4 Challenge: Build for different screen sizes

…

6.5 Good luck!

…

7. What's next?

…