In Android, a TextField is used to take text-based input from the user to perform a particular task. This text can be general text or be specific to characters, numbers, etc. However, this is not the only purpose of implementing a TextField. A TextField can also be converted into a drop-down menu displaying options to be selected by the user. So in this article, we will show you how you could create a Drop-Down Menu in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
package com.geeksforgeeks.jcdropdownmenu
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.toSize
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContent {
// Calling the composable function
// to display element and its contents
MainContent()
}
}
} // Creating a composable // function to display Top Bar @Composable fun MainContent() { Scaffold(
topBar = { TopAppBar(title = { Text( "GFG | Drop Down Menu" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) },
content = { MyContent() }
)
} // Creating a composable function // to create an Outlined Text Field // Calling this function as content // in the above function @Composable fun MyContent(){ // Declaring a boolean value to store
// the expanded state of the Text Field
var mExpanded by remember { mutableStateOf( false ) }
// Create a list of cities
val mCities = listOf( "Delhi" , "Mumbai" , "Chennai" , "Kolkata" , "Hyderabad" , "Bengaluru" , "Pune" )
// Create a string value to store the selected city
var mSelectedText by remember { mutableStateOf( "" ) }
var mTextFieldSize by remember { mutableStateOf(Size.Zero)}
// Up Icon when expanded and down icon when collapsed
val icon = if (mExpanded)
Icons.Filled.KeyboardArrowUp
else
Icons.Filled.KeyboardArrowDown
Column(Modifier.padding( 20 .dp)) {
// Create an Outlined Text Field
// with icon and not expanded
OutlinedTextField(
value = mSelectedText,
onValueChange = { mSelectedText = it },
modifier = Modifier
.fillMaxWidth()
.onGloballyPositioned { coordinates ->
// This value is used to assign to
// the DropDown the same width
mTextFieldSize = coordinates.size.toSize()
},
label = {Text( "Label" )},
trailingIcon = {
Icon(icon, "contentDescription" ,
Modifier.clickable { mExpanded = !mExpanded })
}
)
// Create a drop-down menu with list of cities,
// when clicked, set the Text Field text as the city selected
DropdownMenu(
expanded = mExpanded,
onDismissRequest = { mExpanded = false },
modifier = Modifier
.width(with(LocalDensity.current){mTextFieldSize.width.toDp()})
) {
mCities.forEach { label ->
DropdownMenuItem(onClick = {
mSelectedText = label
mExpanded = false
}) {
Text(text = label)
}
}
}
}
} // For displaying preview in // the Android Studio IDE emulator @Preview (showBackground = true )
@Composable fun DefaultPreview() { MainContent()
} |
Output:
In the below video, you can see that we created a Drop-Down Menu and we are able to select the available options.