Android Day 19 How to collapse the dropdown menu?

Let's cut the chase : We are going to use state variable to control it.

For example, using onDismissRequest = { translatedSelectionExpanded = false } can help you with the dismissRequest action (the action when you click anywhere else), then the dropdown will be dismissed.

And We also need when you select any thing , the drop down automatic dismissed as well.

package com.williamjiamin.ourowncomposable

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.ui.Alignment
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
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.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.williamjiamin.ourowncomposable.ui.theme.OurOwnComposableTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            OurOwnComposableTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    StackOfGreetings()
                }
            }
        }
    }
}

@Composable
fun StackOfGreetings() {

    Column (
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally
    )
    {
        UserInputField()
    }

}

@Composable
fun UserInputField() {

    var originalText by remember { mutableStateOf("") }
    var translatedText by remember { mutableStateOf("") }
    var originalLanguage by remember { mutableStateOf("Spanish") }
    var translatedLanguage by remember { mutableStateOf("English") }
    var originalSelectionExpanded by remember { mutableStateOf(false) }
    var translatedSelectionExpanded by remember { mutableStateOf(false) }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(color = Color.White),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,

    ) {

        Text(
            text = "Translator",
            style = TextStyle(
                fontFamily = FontFamily.Cursive,
                fontSize = 64.sp
            )
        )

        Spacer(modifier = Modifier.height(16.dp))

        Image(
            painter = painterResource(id = R.drawable.logo),
            contentDescription = "William Jiamin",
            modifier = Modifier
                .size(150.dp)
                .clip(CircleShape)
        )

        Spacer(modifier = Modifier.height(16.dp))
        Text("Please Enter the Text You need to Translate:")
        Spacer(modifier = Modifier.height(16.dp))
        OutlinedTextField(value = originalText, onValueChange = {
            originalText = it
        }, label = { Text("Input Here")
        })

        Spacer(modifier = Modifier.height(16.dp))

        val context = LocalContext.current

        Row(
            modifier = Modifier,
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {

            Box {
                Button(
                    onClick = { originalSelectionExpanded = true },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color.LightGray,
                        contentColor = Color.Black
                    )
                ) {
                    Text(originalLanguage)
                    Icon(Icons.Default.ArrowDropDown, contentDescription = "Arrow Down")
                }

                DropdownMenu(expanded = originalSelectionExpanded , onDismissRequest = { originalSelectionExpanded = false }) {
                    DropdownMenuItem(text = { Text("English") }, onClick = {
                        originalLanguage = "English"
                        Toast.makeText(context, "You have Select English as the Language to be Translated!", Toast.LENGTH_SHORT).show()
                        originalSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Spanish") }, onClick = {
                        originalLanguage = "Spanish"
                        Toast.makeText(context, "You have Select Spanish as the Language to be Translated!", Toast.LENGTH_SHORT).show()
                        originalSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Chinese") }, onClick = {
                        originalLanguage = "Chinese"
                        Toast.makeText(context, "You have Select Chinese as the Language to be Translated!", Toast.LENGTH_SHORT).show()
                        originalSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Japanese") }, onClick = {
                        originalLanguage = "Japanese"
                        Toast.makeText(context, "You have Select Japanese as the Language to be Translated!", Toast.LENGTH_SHORT).show()
                        originalSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Korean") }, onClick = {
                        originalLanguage = "Korean"
                        Toast.makeText(context, "You have Select Korean as the Language to be Translated!", Toast.LENGTH_SHORT).show()
                        originalSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Hindi") }, onClick = {
                        originalLanguage = "Hindi"
                        Toast.makeText(context, "You have Select Hindi as the Language to be Translated!", Toast.LENGTH_SHORT).show()
                        originalSelectionExpanded = false
                    })
                }
            }

            // Spacer(modifier = Modifier.width(16.dp))

            Icon(
                imageVector = Icons.Filled.ArrowForward,
                contentDescription = "Arrow Forward",
                modifier = Modifier.padding(16.dp),
            )

            Box {
                Button(
                    onClick = { translatedSelectionExpanded = true },
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color.LightGray,
                        contentColor = Color.Black
                    )
                ) {
                    Text(translatedLanguage)
                    Icon(Icons.Default.ArrowDropDown, contentDescription = "Arrow Down")
                }

                DropdownMenu(expanded = translatedSelectionExpanded , onDismissRequest = { translatedSelectionExpanded = false }) {
                    DropdownMenuItem(text = { Text("English") }, onClick = {
                        translatedLanguage = "English"
                        Toast.makeText(context, "You have Select English as the Language Translated!", Toast.LENGTH_SHORT).show()
                        translatedSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Spanish") }, onClick = {
                        translatedLanguage = "Spanish"
                        Toast.makeText(context, "You have Select Spanish as the Language Translated!", Toast.LENGTH_SHORT).show()
                        translatedSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Chinese") }, onClick = {
                        translatedLanguage = "Chinese"
                        Toast.makeText(context, "You have Select Chinese as the Language Translated!", Toast.LENGTH_SHORT).show()
                        translatedSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Japanese") }, onClick = {
                        translatedLanguage = "Japanese"
                        Toast.makeText(context, "You have Select Japanese as the Language Translated!", Toast.LENGTH_SHORT).show()
                        translatedSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Korean") }, onClick = {
                        translatedLanguage = "Korean"
                        Toast.makeText(context, "You have Select Korean as the Language Translated!", Toast.LENGTH_SHORT).show()
                        translatedSelectionExpanded = false
                    })
                    DropdownMenuItem(text = { Text("Hindi") }, onClick = {
                        translatedLanguage = "Hindi"
                        Toast.makeText(context, "You have Select Hindi as the Language Translated!", Toast.LENGTH_SHORT).show()
                        translatedSelectionExpanded = false
                    })
                }
            }
        }

        Spacer(modifier = Modifier.height(16.dp))

        OutlinedTextField(value = translatedText, onValueChange = {
            translatedText = it
        }, label = { Text("Output Here")
        })
    }
}

@Preview(showBackground = true)
@Composable
fun StackOfGreetingsPreview() {
    OurOwnComposableTheme {
    StackOfGreetings()
    }
}

And you can achive the selection as well as dismiss/ collapse the dropdown.