Skip to content

Commit b883fb9

Browse files
authored
Adding badge examples (#277)
* Adding badge examples * Apply Spotless * Tweaks * Moving conditional so there's always a BadgedBox * Apply Spotless --------- Co-authored-by: jakeroseman <[email protected]>
1 parent e34c891 commit b883fb9

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/SnippetsActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import androidx.navigation.compose.composable
2929
import androidx.navigation.compose.rememberNavController
3030
import com.example.compose.snippets.animations.AnimationExamplesScreen
3131
import com.example.compose.snippets.components.AppBarExamples
32+
import com.example.compose.snippets.components.BadgeExamples
3233
import com.example.compose.snippets.components.ButtonExamples
3334
import com.example.compose.snippets.components.CheckboxExamples
3435
import com.example.compose.snippets.components.ChipExamples
@@ -99,6 +100,7 @@ class SnippetsActivity : ComponentActivity() {
99100
}
100101
TopComponentsDestination.CheckboxExamples -> CheckboxExamples()
101102
TopComponentsDestination.DividerExamples -> DividerExamples()
103+
TopComponentsDestination.BadgeExamples -> BadgeExamples()
102104
}
103105
}
104106
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.compose.snippets.components
18+
19+
import androidx.compose.foundation.layout.Arrangement
20+
import androidx.compose.foundation.layout.Column
21+
import androidx.compose.foundation.layout.fillMaxSize
22+
import androidx.compose.foundation.layout.padding
23+
import androidx.compose.material.icons.Icons
24+
import androidx.compose.material.icons.filled.Mail
25+
import androidx.compose.material.icons.filled.ShoppingCart
26+
import androidx.compose.material3.Badge
27+
import androidx.compose.material3.BadgedBox
28+
import androidx.compose.material3.Button
29+
import androidx.compose.material3.Icon
30+
import androidx.compose.material3.Text
31+
import androidx.compose.runtime.Composable
32+
import androidx.compose.runtime.getValue
33+
import androidx.compose.runtime.mutableStateOf
34+
import androidx.compose.runtime.remember
35+
import androidx.compose.runtime.setValue
36+
import androidx.compose.ui.Alignment
37+
import androidx.compose.ui.Modifier
38+
import androidx.compose.ui.graphics.Color
39+
import androidx.compose.ui.text.font.FontWeight
40+
import androidx.compose.ui.tooling.preview.Preview
41+
import androidx.compose.ui.unit.dp
42+
43+
@Preview
44+
@Composable
45+
fun BadgeExamples() {
46+
Column(
47+
modifier = Modifier
48+
.padding(16.dp)
49+
.fillMaxSize(),
50+
verticalArrangement = Arrangement.spacedBy(32.dp),
51+
horizontalAlignment = Alignment.CenterHorizontally,
52+
) {
53+
Text("Minimal badge example", fontWeight = FontWeight.Bold)
54+
BadgeExample()
55+
Text("Badge number example", fontWeight = FontWeight.Bold)
56+
BadgeInteractiveExample()
57+
}
58+
}
59+
60+
@Preview
61+
// [START android_compose_components_badge]
62+
@Composable
63+
fun BadgeExample() {
64+
BadgedBox(
65+
badge = {
66+
Badge()
67+
}
68+
) {
69+
Icon(
70+
imageVector = Icons.Filled.Mail,
71+
contentDescription = "Email"
72+
)
73+
}
74+
}
75+
// [END android_compose_components_badge]
76+
77+
@Preview
78+
// [START android_compose_components_badgeinteractive]
79+
@Composable
80+
fun BadgeInteractiveExample() {
81+
var itemCount by remember { mutableStateOf(0) }
82+
83+
Column(
84+
verticalArrangement = Arrangement.spacedBy(16.dp)
85+
) {
86+
BadgedBox(
87+
badge = {
88+
if (itemCount > 0) {
89+
Badge(
90+
containerColor = Color.Red,
91+
contentColor = Color.White
92+
) {
93+
Text("$itemCount")
94+
}
95+
}
96+
}
97+
) {
98+
Icon(
99+
imageVector = Icons.Filled.ShoppingCart,
100+
contentDescription = "Shopping cart",
101+
)
102+
}
103+
Button(onClick = { itemCount++ }) {
104+
Text("Add item")
105+
}
106+
}
107+
}
108+
// [END android_compose_components_badgeinteractive]

compose/snippets/src/main/java/com/example/compose/snippets/navigation/Destination.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ enum class TopComponentsDestination(val route: String, val title: String) {
3939
AppBarExamples("appBarExamples", "App bars"),
4040
CheckboxExamples("checkboxExamples", "Checkbox"),
4141
DividerExamples("dividerExamples", "Dividers"),
42+
BadgeExamples("badgeExamples", "Badges"),
4243
}

0 commit comments

Comments
 (0)