-
Notifications
You must be signed in to change notification settings - Fork 289
Basic drop down menu examples #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e4e4dec
Basic drop down menu examples
jakeroseman f1ad34c
Apply Spotless
jakeroseman 2663794
Very minor formatting changes that spotless didn't do itself
jakeroseman f165a61
Add missing content description
jakeroseman a4b47ca
More very minor formatting changes that spotless didn't do itself
jakeroseman 246e68b
Apply Spotless
jakeroseman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
compose/snippets/src/main/java/com/example/compose/snippets/components/Menu.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| /* | ||
| * Copyright 2024 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.compose.snippets.components | ||
|
|
||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.material.icons.Icons | ||
| import androidx.compose.material.icons.filled.ArrowDropDown | ||
| import androidx.compose.material.icons.filled.MoreVert | ||
| import androidx.compose.material3.Button | ||
| import androidx.compose.material3.DropdownMenu | ||
| import androidx.compose.material3.DropdownMenuItem | ||
| import androidx.compose.material3.ExperimentalMaterial3Api | ||
| import androidx.compose.material3.FloatingActionButton | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.IconButton | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.material3.TextField | ||
| import androidx.compose.material3.TopAppBar | ||
| 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.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| @Composable | ||
| fun MenusExamples() { | ||
| var currentExample by remember { mutableStateOf<(@Composable () -> Unit)?>(null) } | ||
|
|
||
| // Display the current example and the button to close it. | ||
| Box(modifier = Modifier.fillMaxSize()) { | ||
| currentExample?.let { | ||
| it() | ||
| FloatingActionButton( | ||
| onClick = { currentExample = null }, | ||
| modifier = Modifier | ||
| .align(Alignment.BottomEnd) | ||
| .padding(16.dp) | ||
| ) { | ||
| Text(text = "Close example", modifier = Modifier.padding(16.dp)) | ||
| } | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // Display the list of available examples. | ||
| Column(modifier = Modifier.padding(16.dp)) { | ||
| Button(onClick = { currentExample = { BasicDropdownMenu() } }) { | ||
| Text("Action Bar Menu") | ||
| } | ||
| Button(onClick = { currentExample = { TextFieldDropdownMenu() } }) { | ||
| Text("Options Menu") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| // [START android_compose_components_basicdropdownmenu] | ||
| @Composable | ||
| fun BasicDropdownMenu() { | ||
| var shouldDisplayMenu by remember { mutableStateOf(false) } | ||
|
|
||
| TopAppBar( | ||
| title = { Text(text = "Basic menu example") }, | ||
| actions = { | ||
| IconButton(onClick = { shouldDisplayMenu = !shouldDisplayMenu }) { | ||
| Icon(Icons.Default.MoreVert, "Overflow menu button") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Content description should describe the action. Change to "More options". |
||
| } | ||
| DropdownMenu( | ||
| expanded = shouldDisplayMenu, | ||
| onDismissRequest = { shouldDisplayMenu = false } | ||
| ) { | ||
| DropdownMenuItem( | ||
| text = { Text("Refresh") }, | ||
| onClick = { /* Handle refresh! */ } | ||
| ) | ||
| DropdownMenuItem( | ||
| text = { Text("Settings") }, | ||
| onClick = { /* Handle settings! */ } | ||
| ) | ||
| } | ||
| } | ||
| ) | ||
| } | ||
| // [END android_compose_components_basicdropdownmenu] | ||
|
|
||
| @Preview | ||
| @Composable | ||
| private fun BasicDropdownMenuPreview() { | ||
| BasicDropdownMenu() | ||
| } | ||
|
|
||
| // [START android_compose_components_textfielddropdownmenu] | ||
| @Composable | ||
| fun TextFieldDropdownMenu() { | ||
| var expanded by remember { mutableStateOf(false) } | ||
| var selectedText by remember { mutableStateOf("") } | ||
| val options = listOf("Option 1", "Option 2", "Option 3") | ||
|
|
||
| Box(modifier = Modifier.fillMaxWidth()) { | ||
| TextField( | ||
| value = selectedText, | ||
| onValueChange = { selectedText = it }, | ||
| modifier = Modifier.fillMaxWidth(), | ||
| label = { Text("Select an option") }, | ||
| trailingIcon = { | ||
| IconButton(onClick = { expanded = true }) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you want the whole text field to be clickable, not just the arrow? |
||
| Icon(Icons.Filled.ArrowDropDown, "Dropdown arrow") | ||
| } | ||
| }, | ||
| readOnly = true | ||
| ) | ||
| DropdownMenu( | ||
| expanded = expanded, | ||
| onDismissRequest = { expanded = false }, | ||
| modifier = Modifier.fillMaxWidth() | ||
| ) { | ||
| options.forEach { option -> | ||
| DropdownMenuItem( | ||
| text = { Text(option) }, | ||
| onClick = { | ||
| selectedText = option | ||
| expanded = false | ||
| } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END android_compose_components_textfielddropdownmenu] | ||
|
|
||
| @Preview | ||
| @Composable | ||
| private fun TextFieldMenuPreview() { | ||
| TextFieldDropdownMenu() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend adding a second action, thereby making it clearer that the IconButton+DropdownMenu should be wrapped by a Box to correctly set the anchor for the DropdownMenu: