Skip to content

Practice problem: Click behavior #45

@GruFRe

Description

@GruFRe

I dont understand the following:

In step 5 "Implement the App" you write there isnt a button in this app and we should read through the clickable dokumentation to make every composable clickable.
When i take a look at the solution code after trying to get it working for hours there is a button added...
I also can seond that i this codelab are a lot of new things wich were never refered to before....
btw i dont understand any word in your documentation...neither the weight one wich was refered before, not this one or any other one so this will not help me in any way.

So can anyone explain it to me?

here is how i implemented it till now...

package com.example.lemonadecompose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.lemonadecompose.ui.theme.LemonadeComposeTheme

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

@composable
@Preview
fun LemonadeApp() {
MakeLemonade(
modifier = Modifier
.fillMaxSize()
.wrapContentSize(Alignment.Center)
)
}

@composable
fun MakeLemonade(modifier: Modifier = Modifier) {

var lemonState by remember { mutableStateOf(1) }
var toSqueeze by remember {mutableStateOf(0)}

var instructions = R.string.lemon_select
var imageResource = R.drawable.lemon_tree
var description = R.string.lemon_tree_description

Surface(
    modifier = Modifier.fillMaxSize(),
    color = MaterialTheme.colorScheme.background
) {
    Column(
        modifier = modifier,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = painterResource(imageResource),
            contentDescription = stringResource(id = description),
            modifier = Modifier
                .padding(bottom = 16.dp)
                .height(280.dp)
                .width(280.dp)
                .clickable { when (lemonState) {
                        1 -> {
                            lemonState++
                            instructions = R.string.lemon_squeeze
                            imageResource = R.drawable.lemon_squeeze
                            description = R.string.lemon_description
                            toSqueeze = (2..4).random()
                        }

                        2 -> {
                            if (toSqueeze == 0) {
                                lemonState++
                                instructions = R.string.lemonade_drink
                                imageResource = R.drawable.lemon_drink
                                description = R.string.full_glass_description
                            } else {
                                toSqueeze--
                            }
                        }

                        3 -> {
                            lemonState++
                            instructions = R.string.start_again
                            imageResource = R.drawable.lemon_restart
                            description = R.string.empty_glass_description
                        }

                        else -> {
                            lemonState = 1
                            instructions = R.string.lemon_select
                            imageResource = R.drawable.lemon_tree
                            description = R.string.lemon_tree_description
                        }
                    }
                }
                .background(
                    shape = RoundedCornerShape(100.dp),
                    color = Color.Blue
                )
        )
        Text(
            text = stringResource(instructions),
            fontSize = 18.sp
        )
        //only works with this etra composable that prints the lemonState
        //doesnt work if i set the clickable modifier in the upper text composable
        Text(
            text = lemonState.toString(),
            modifier = Modifier.clickable { lemonState++ }
        )
    }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions