-
Notifications
You must be signed in to change notification settings - Fork 287
Tooltip component examples #373
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
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
27aaca6
Tooltip component examples
jakeroseman c076771
Apply Spotless
jakeroseman b2ebc5a
Addressing PR comments
jakeroseman 39e44bd
use LaunchedEffect to fix tooltip bug
jakeroseman 52116f2
Merge branch 'tooltips' of https://github.com/android/snippets into t…
jakeroseman 80be656
Apply Spotless
jakeroseman 3d4a520
Updated content descriptions
jakeroseman 0947fd9
Merge branch 'tooltips' of https://github.com/android/snippets into t…
jakeroseman 08578cf
Merge branch 'main' into tooltips
jakeroseman ea571ef
Merge branch 'main' into tooltips
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
177 changes: 177 additions & 0 deletions
177
compose/snippets/src/main/java/com/example/compose/snippets/components/Tooltips.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,177 @@ | ||
| /* | ||
| * 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.Arrangement | ||
| import androidx.compose.foundation.layout.Row | ||
| 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.Camera | ||
| import androidx.compose.material.icons.filled.Favorite | ||
| import androidx.compose.material.icons.filled.Info | ||
| import androidx.compose.material3.ExperimentalMaterial3Api | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.IconButton | ||
| import androidx.compose.material3.PlainTooltip | ||
| import androidx.compose.material3.RichTooltip | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.material3.TextButton | ||
| import androidx.compose.material3.TooltipBox | ||
| import androidx.compose.material3.TooltipDefaults | ||
| import androidx.compose.material3.rememberTooltipState | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.text.style.TextAlign | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.DpSize | ||
| import androidx.compose.ui.unit.dp | ||
|
|
||
| @Composable | ||
| fun TooltipExamples() { | ||
| Text( | ||
| "Long press an icon to see the tooltip.", | ||
| modifier = Modifier.fillMaxWidth().padding(16.dp), | ||
| textAlign = TextAlign.Center | ||
| ) | ||
| Row( | ||
| horizontalArrangement = Arrangement.SpaceEvenly, | ||
| verticalAlignment = Alignment.CenterVertically, | ||
| modifier = Modifier.fillMaxSize() | ||
| ) { | ||
| PlainTooltipExample() | ||
| RichTooltipExample() | ||
| AdvancedRichTooltipExample() | ||
| } | ||
| } | ||
|
|
||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| // [START android_compose_components_plaintooltipexample] | ||
| @Composable | ||
| fun PlainTooltipExample( | ||
| modifier: Modifier = Modifier, | ||
| plainTooltipText: String = "Add to favorites" | ||
| ) { | ||
| TooltipBox( | ||
| modifier = modifier, | ||
| positionProvider = TooltipDefaults.rememberPlainTooltipPositionProvider(), | ||
| tooltip = { | ||
| PlainTooltip { Text(plainTooltipText) } | ||
| }, | ||
| state = rememberTooltipState() | ||
| ) { | ||
| IconButton(onClick = { /* Icon button's click event */ }) { | ||
| Icon( | ||
| imageVector = Icons.Filled.Favorite, | ||
| contentDescription = "Localized Description" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| // [END android_compose_components_plaintooltipexample] | ||
|
|
||
| @Preview | ||
| @Composable | ||
| private fun PlainTooltipSamplePreview() { | ||
| PlainTooltipExample() | ||
| } | ||
|
|
||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| // [START android_compose_components_richtooltipexample] | ||
| @Composable | ||
| fun RichTooltipExample( | ||
| modifier: Modifier = Modifier, | ||
| richTooltipSubheadText: String = "Rich Tooltip", | ||
| richTooltipText: String = "Rich tooltips supports multiple lines of informational text." | ||
| ) { | ||
| val tooltipState = rememberTooltipState(isPersistent = true) | ||
|
|
||
| TooltipBox( | ||
| modifier = modifier, | ||
| positionProvider = TooltipDefaults.rememberRichTooltipPositionProvider(), | ||
| tooltip = { | ||
| RichTooltip( | ||
| title = { Text(richTooltipSubheadText) } | ||
| ) { | ||
| Text(richTooltipText) | ||
| } | ||
| }, state = tooltipState | ||
| ) { | ||
| IconButton(onClick = { /* Icon button's click event */ }) { | ||
| Icon( | ||
| imageVector = Icons.Filled.Info, | ||
| contentDescription = "Localized Description" | ||
jakeroseman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| } | ||
| } | ||
| } | ||
| // [END android_compose_components_richtooltipexample] | ||
|
|
||
| @Preview | ||
| @Composable | ||
| private fun RichTooltipSamplePreview() { | ||
| RichTooltipExample() | ||
| } | ||
|
|
||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| // [START android_compose_components_advancedrichtooltipexample] | ||
| @Composable | ||
| fun AdvancedRichTooltipExample( | ||
| modifier: Modifier = Modifier, | ||
| richTooltipSubheadText: String = "Custom Rich Tooltip", | ||
| richTooltipText: String = "Rich tooltips supports multiple lines of informational text.", | ||
| richTooltipActionText: String = "Dismiss" | ||
| ) { | ||
| val tooltipState = rememberTooltipState(isPersistent = true) | ||
|
|
||
| TooltipBox( | ||
| modifier = modifier, | ||
| positionProvider = TooltipDefaults.rememberRichTooltipPositionProvider(), | ||
| tooltip = { | ||
| RichTooltip( | ||
| title = { Text(richTooltipSubheadText) }, | ||
| action = { | ||
| Row { | ||
| TextButton(onClick = { /* Do something... */ }) { | ||
jakeroseman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Text(richTooltipActionText) | ||
| } | ||
| TextButton(onClick = { /* Do something... */ }) { | ||
| Text("Learn more") | ||
jakeroseman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| }, | ||
| caretSize = DpSize(32.dp, 16.dp) | ||
| ) { | ||
| Text(richTooltipText) | ||
| } | ||
| }, | ||
| state = tooltipState | ||
| ) { | ||
| IconButton(onClick = { /* Icon button's click event */ }) { | ||
| Icon(imageVector = Icons.Filled.Camera, contentDescription = "Localized Description") | ||
| } | ||
| } | ||
| } | ||
| // [END android_compose_components_advancedrichtooltipexample] | ||
|
|
||
| @Preview | ||
| @Composable | ||
| private fun RichTooltipWithCustomCaretSamplePreview() { | ||
| AdvancedRichTooltipExample() | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.