-
Notifications
You must be signed in to change notification settings - Fork 289
Basic menu examples #371
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
Merged
Merged
Basic menu examples #371
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a59caf2
basic menu examples
jakeroseman 937730d
Make DropdownMenuWithDetails toggle expanded on click
jakeroseman dcb8e50
Apply Spotless
jakeroseman 56e88c8
Remove unneeded dependencies
jakeroseman 20f6c4b
Merge branch 'menus-basic' of https://github.com/android/snippets int…
jakeroseman bf58090
Remove unneeded imports
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
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
201 changes: 201 additions & 0 deletions
201
compose/snippets/src/main/java/com/example/compose/snippets/components/Menus.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,201 @@ | ||
| 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.automirrored.filled.Help | ||
| import androidx.compose.material.icons.automirrored.filled.OpenInNew | ||
| import androidx.compose.material.icons.automirrored.filled.Send | ||
| import androidx.compose.material.icons.automirrored.outlined.Help | ||
| import androidx.compose.material.icons.automirrored.outlined.OpenInNew | ||
| import androidx.compose.material.icons.automirrored.outlined.Send | ||
| import androidx.compose.material.icons.filled.Feedback | ||
| import androidx.compose.material.icons.filled.Info | ||
| import androidx.compose.material.icons.filled.MoreVert | ||
| import androidx.compose.material.icons.filled.Person | ||
| import androidx.compose.material.icons.filled.Settings | ||
| import androidx.compose.material3.Button | ||
| import androidx.compose.material3.DropdownMenu | ||
| import androidx.compose.material3.DropdownMenuItem | ||
| import androidx.compose.material3.FloatingActionButton | ||
| import androidx.compose.material3.HorizontalDivider | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.IconButton | ||
| 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.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| @Composable | ||
| fun MenusExamples() { | ||
| var currentExample by remember { mutableStateOf<(@Composable () -> Unit)?>(null) } | ||
|
|
||
| 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 | ||
| } | ||
|
|
||
| Column(modifier = Modifier.padding(16.dp)) { | ||
| Button(onClick = { currentExample = { MinimalDropdownMenu() } }) { | ||
| Text("Minimal dropdown menu") | ||
| } | ||
| Button(onClick = { currentExample = { LongBasicDropdownMenu() } }) { | ||
| Text("Dropdown menu with many items") | ||
| } | ||
| Button(onClick = { currentExample = { DropdownMenuWithDetails() } }) { | ||
| Text("Dropdown menu with sections and icons") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // [START android_compose_components_minimaldropdownmenu] | ||
| @Composable | ||
| fun MinimalDropdownMenu() { | ||
| var expanded by remember { mutableStateOf(false) } | ||
| Box( | ||
| modifier = Modifier | ||
| .padding(16.dp) | ||
| ) { | ||
| IconButton(onClick = { expanded = !expanded }) { | ||
| Icon(Icons.Default.MoreVert, contentDescription = "More options") | ||
| } | ||
| DropdownMenu( | ||
| expanded = expanded, | ||
| onDismissRequest = { expanded = false } | ||
| ) { | ||
| DropdownMenuItem( | ||
| text = { Text("Option 1") }, | ||
| onClick = { /* Do something... */ } | ||
| ) | ||
| DropdownMenuItem( | ||
| text = { Text("Option 2") }, | ||
| onClick = { /* Do something... */ } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| // [END android_compose_components_minimaldropdownmenu] | ||
|
|
||
| @Preview | ||
| @Composable | ||
| fun MinimalDropdownMenuPreview() { | ||
| MinimalDropdownMenu() | ||
| } | ||
|
|
||
| // [START android_compose_components_longbasicdropdownmenu] | ||
| @Composable | ||
| fun LongBasicDropdownMenu() { | ||
| var expanded by remember { mutableStateOf(false) } | ||
| // Placeholder list of 100 strings for demonstration | ||
| val menuItemData = List(100) { "Option ${it + 1}" } | ||
|
|
||
| Box( | ||
| modifier = Modifier | ||
| .padding(16.dp) | ||
| ) { | ||
| IconButton(onClick = { expanded = !expanded }) { | ||
| Icon(Icons.Default.MoreVert, contentDescription = "More options") | ||
| } | ||
| DropdownMenu( | ||
| expanded = expanded, | ||
| onDismissRequest = { expanded = false } | ||
| ) { | ||
| menuItemData.forEach { option -> | ||
| DropdownMenuItem( | ||
| text = { Text(option) }, | ||
| onClick = { /* Do something... */ } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END android_compose_components_longbasicdropdownmenu] | ||
|
|
||
| @Preview | ||
| @Composable | ||
| fun LongBasicDropdownMenuPreview() { | ||
| LongBasicDropdownMenu() | ||
| } | ||
|
|
||
| // [START android_compose_components_dropdownmenuwithdetails] | ||
| @Composable | ||
| fun DropdownMenuWithDetails() { | ||
| var expanded by remember { mutableStateOf(false) } | ||
|
|
||
| Box( | ||
| modifier = Modifier | ||
| .fillMaxWidth() | ||
| .padding(16.dp) | ||
| ) { | ||
| IconButton(onClick = { expanded = !expanded }) { | ||
| Icon(Icons.Default.MoreVert, contentDescription = "More options") | ||
| } | ||
| DropdownMenu( | ||
| expanded = expanded, | ||
| onDismissRequest = { expanded = false } | ||
| ) { | ||
| // First section | ||
| DropdownMenuItem( | ||
| text = { Text("Profile") }, | ||
| leadingIcon = { Icon(Icons.Default.Person, contentDescription = null) }, | ||
| onClick = { /* Do something... */ } | ||
| ) | ||
| DropdownMenuItem( | ||
| text = { Text("Settings") }, | ||
| leadingIcon = { Icon(Icons.Default.Settings, contentDescription = null) }, | ||
| onClick = { /* Do something... */ } | ||
| ) | ||
|
|
||
| HorizontalDivider() | ||
|
|
||
| // Second section | ||
| DropdownMenuItem( | ||
| text = { Text("Send Feedback") }, | ||
| leadingIcon = { Icon(Icons.Default.Feedback, contentDescription = null) }, | ||
| trailingIcon = { Icon(Icons.AutoMirrored.Outlined.Send, contentDescription = null) }, | ||
| onClick = { /* Do something... */ } | ||
| ) | ||
|
|
||
| HorizontalDivider() | ||
|
|
||
| // Third section | ||
| DropdownMenuItem( | ||
| text = { Text("About") }, | ||
| leadingIcon = { Icon(Icons.Default.Info, contentDescription = null) }, | ||
| onClick = { /* Do something... */ } | ||
| ) | ||
| DropdownMenuItem( | ||
| text = { Text("Help") }, | ||
| leadingIcon = { Icon(Icons.AutoMirrored.Outlined.Help, contentDescription = null) }, | ||
| trailingIcon = { Icon(Icons.AutoMirrored.Outlined.OpenInNew, contentDescription = null) }, | ||
| onClick = { /* Do something... */ } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| // [END android_compose_components_dropdownmenuwithdetails] | ||
|
|
||
| @Preview | ||
| @Composable | ||
| fun DropdownMenuWithDetailsPreview() { | ||
| DropdownMenuWithDetails() | ||
| } | ||
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.