1

I am developing a Question/Answer Quiz App in JetPack Compose. I have the Card as below

Card(modifier = Modifier
        .wrapContentHeight(Alignment.CenterVertically)
        .wrapContentWidth(Alignment.CenterHorizontally)
        .padding(10.dp)
        .width(300.dp)
        .height(600.dp)
        .clip(RoundedCornerShape(8.dp)),
        elevation = 10.dp,
        backgroundColor = Color.White

    )
    {

        Column(
            modifier = Modifier
                .wrapContentHeight(Alignment.CenterVertically)
                .wrapContentWidth(Alignment.CenterHorizontally)
                .padding(8.dp),
            horizontalAlignment = Alignment.Start,
            verticalArrangement = Arrangement.Top

            ){

            Text(
                text = "Question : " + query.question_id,
                style = Typography.h1
            )
            Spacer(modifier = Modifier.height(2.dp))
            Text(
                text = query.question,
                style = Typography.subtitle1

            )
            Spacer(modifier = Modifier.height(5.dp))
            Text(
                text = "Options",
                style = Typography.h1
            )
            Spacer(modifier = Modifier.height(2.dp))
            OptionsDetailsList(lstOptions = lstOptions)
            
            Spacer(modifier = Modifier.height(10.dp))
            
            Button(onClick = {
            }
            ) {
                Text(text = "View Answer")
            }
            Spacer(modifier = Modifier.height(10.dp))
        }
    }
}

The Answer List is a LazyColumn as below

@Composable
fun OptionsDetailsList(lstOptions: List<CertAnswers>){
    Log.d("ListOptions Count" , lstOptions.size.toString())
    LazyColumn(){
        item {
            Spacer(modifier = Modifier.requiredHeight(1.dp))
        }
        items(lstOptions){
                item ->
            Text(text = item.answer,
                modifier=Modifier.padding(3.dp),
                style = Typography.subtitle1,
            )
            Spacer(modifier = Modifier.requiredHeight(1.dp))
        }
    }
}

My Answers DataModel is as Below

data class CertAnswers(
    @PrimaryKey (autoGenerate = true)
    val id : Int,
    var question_id : Int,
    val ans_title: String,
    val answer : String,
    var isSolution: Boolean
)

With isSolution I can get whether an option is right answer. But I want to show only on the button click and update LazyColumn to show the relevant option in different color. I tried to call the OptionsDetailsList method again on button click but its not working. How can i update the LazyColumn on Button Click within CardView.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
srituts
  • 53
  • 8

1 Answers1

0

You need to store a state value in your CardView indicating wether the button was tapped. rememberSaveable will make sure it's saved during recompositions and scrolling. I pass query.question_id as a key: in case it'll change most probably the value should be reinitialized. Check out more about state in compose in documentation

var answerRevealed by rememberSaveable(query.question_id) { mutableStateOf(false) }

You can change the background of the lazy column elements depending on this state. p.s. I suggest you use Modifier as the last argument, so you don't need a comma at the end and you can add/remove/reorder modifiers easily:

@Composable
fun OptionsDetailsList(answerRevealed: Boolean, lstOptions: List<CertAnswers>) {
    Log.d("ListOptions Count", lstOptions.size.toString())
    LazyColumn() {
        item {
            Spacer(modifier = Modifier.requiredHeight(1.dp))
        }
        items(lstOptions) { item ->
            Text(
                text = item.answer,
                style = Typography().subtitle1,
                modifier = Modifier
                    .padding(3.dp)
                    .background(
                        if (answerRevealed) {
                            if (item.isSolution) {
                                Color.Green
                            } else {
                                Color.Red
                            }
                        } else {
                            Color.Transparent
                        }
                    )
            )
            Spacer(modifier = Modifier.requiredHeight(1.dp))
        }
    }
}

In your button just set this state to true. You can also hide the button once it's tapped, or change the text.

// hide after onClick
if (!answerRevealed) {
    Button(onClick = {
        answerRevealed = true
    }
    ) {
        Text(text = "View Answer")
    }
}
// or change text
Button(onClick = {
    answerRevealed = true
}
) {
    Text(text = if (answerRevealed) "Hide Answer" else "View Answer")
}
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220