-
Notifications
You must be signed in to change notification settings - Fork 289
Icon button snippets #512
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
Icon button snippets #512
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fc37f3c
Adding icon button snippets
wardlauren 350dd61
Apply Spotless
wardlauren fdb36f0
Changing M3 extended icons to hardcoded drawables in IconButton code
wardlauren 12d0e82
Completing merge
wardlauren ac96ad1
Apply Spotless
wardlauren 4acfee0
Adding region tags
wardlauren 8a820bc
Merge branch 'icon-button' of https://github.com/android/snippets int…
wardlauren 07ed52e
Remove unused import
wardlauren 8522d30
Updating to hardcoded drawables
wardlauren 2afa1cb
Apply Spotless
wardlauren 7c8bb39
Merge branch 'main' into icon-button
wardlauren f39bb59
Merge branch 'main' into icon-button
wardlauren 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
127 changes: 127 additions & 0 deletions
127
compose/snippets/src/main/java/com/example/compose/snippets/components/IconButton.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,127 @@ | ||
| /* | ||
| * Copyright 2025 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.interaction.MutableInteractionSource | ||
| import androidx.compose.foundation.interaction.collectIsPressedAsState | ||
| import androidx.compose.foundation.layout.Row | ||
| import androidx.compose.foundation.layout.Spacer | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.material.icons.Icons | ||
| import androidx.compose.material.icons.filled.Favorite | ||
| import androidx.compose.material.icons.outlined.FavoriteBorder | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.IconButton | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableIntStateOf | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.rememberUpdatedState | ||
| import androidx.compose.runtime.saveable.rememberSaveable | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.res.painterResource | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import com.example.compose.snippets.R | ||
| import kotlinx.coroutines.delay | ||
|
|
||
| // [START android_compose_components_togglebuttonexample] | ||
| @Preview | ||
| @Composable | ||
| fun ToggleIconButtonExample() { | ||
| // isToggled initial value should be read from a view model or persistent storage. | ||
| var isToggled by rememberSaveable { mutableStateOf(false) } | ||
|
|
||
| IconButton( | ||
| onClick = { isToggled = !isToggled } | ||
| ) { | ||
| Icon( | ||
| imageVector = if (isToggled) Icons.Filled.Favorite else Icons.Outlined.FavoriteBorder, | ||
| contentDescription = if (isToggled) "Selected icon button" else "Unselected icon button." | ||
| ) | ||
| } | ||
| } | ||
| // [END android_compose_components_togglebuttonexample] | ||
|
|
||
| // [START android_compose_components_iconbutton] | ||
| @Composable | ||
| fun MomentaryIconButton( | ||
| unselectedImage: Int, | ||
| selectedImage: Int, | ||
| contentDescription: String, | ||
| modifier: Modifier = Modifier, | ||
| stepDelay: Long = 100L, // Minimum value is 1L milliseconds. | ||
| onClick: () -> Unit | ||
| ) { | ||
| val interactionSource = remember { MutableInteractionSource() } | ||
| val isPressed by interactionSource.collectIsPressedAsState() | ||
| val pressedListener by rememberUpdatedState(onClick) | ||
|
|
||
| LaunchedEffect(isPressed) { | ||
| while (isPressed) { | ||
| delay(stepDelay.coerceIn(1L, Long.MAX_VALUE)) | ||
| pressedListener() | ||
| } | ||
| } | ||
|
|
||
| IconButton( | ||
| modifier = modifier, | ||
| onClick = onClick, | ||
| interactionSource = interactionSource | ||
| ) { | ||
| Icon( | ||
| painter = if (isPressed) painterResource(id = selectedImage) else painterResource(id = unselectedImage), | ||
| contentDescription = contentDescription, | ||
| ) | ||
| } | ||
| } | ||
| // [END android_compose_components_iconbutton] | ||
|
|
||
| // [START android_compose_components_momentaryiconbuttons] | ||
| @Preview() | ||
| @Composable | ||
| fun MomentaryIconButtonExample() { | ||
| var pressedCount by remember { mutableIntStateOf(0) } | ||
|
|
||
| Row( | ||
| modifier = Modifier.fillMaxWidth(), | ||
| verticalAlignment = Alignment.CenterVertically | ||
| ) { | ||
| MomentaryIconButton( | ||
| unselectedImage = R.drawable.fast_rewind, | ||
| selectedImage = R.drawable.fast_rewind_filled, | ||
| stepDelay = 100L, | ||
| onClick = { pressedCount -= 1 }, | ||
| contentDescription = "Decrease count button" | ||
| ) | ||
| Spacer(modifier = Modifier) | ||
| Text("advanced by $pressedCount frames") | ||
| Spacer(modifier = Modifier) | ||
| MomentaryIconButton( | ||
| unselectedImage = R.drawable.fast_forward, | ||
| selectedImage = R.drawable.fast_forward_filled, | ||
| contentDescription = "Increase count button", | ||
| stepDelay = 100L, | ||
| onClick = { pressedCount += 1 } | ||
| ) | ||
| } | ||
| } | ||
| // [END android_compose_components_momentaryiconbuttons] | ||
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,9 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="960" | ||
| android:viewportHeight="960"> | ||
| <path | ||
| android:pathData="M100,720v-480l360,240 -360,240ZM500,720v-480l360,240 -360,240ZM180,480ZM580,480ZM180,570 L316,480 180,390v180ZM580,570 L716,480 580,390v180Z" | ||
| android:fillColor="#e3e3e3"/> | ||
| </vector> |
9 changes: 9 additions & 0 deletions
9
compose/snippets/src/main/res/drawable/fast_forward_filled.xml
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,9 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="960" | ||
| android:viewportHeight="960"> | ||
| <path | ||
| android:pathData="M100,720v-480l360,240 -360,240ZM500,720v-480l360,240 -360,240Z" | ||
| android:fillColor="#e3e3e3"/> | ||
| </vector> |
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,9 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="960" | ||
| android:viewportHeight="960"> | ||
| <path | ||
| android:pathData="M860,720 L500,480l360,-240v480ZM460,720L100,480l360,-240v480ZM380,480ZM780,480ZM380,570v-180l-136,90 136,90ZM780,570v-180l-136,90 136,90Z" | ||
| android:fillColor="#e3e3e3"/> | ||
| </vector> |
9 changes: 9 additions & 0 deletions
9
compose/snippets/src/main/res/drawable/fast_rewind_filled.xml
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,9 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="960" | ||
| android:viewportHeight="960"> | ||
| <path | ||
| android:pathData="M860,720 L500,480l360,-240v480ZM460,720L100,480l360,-240v480Z" | ||
| android:fillColor="#e3e3e3"/> | ||
| </vector> |
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.