Skip to content

Commit 63b82c2

Browse files
committed
Adding SearchBar examples
1 parent 0d07b08 commit 63b82c2

File tree

41 files changed

+630
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+630
-0
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package com.example.compose.snippets.components
2+
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.Arrangement
5+
import androidx.compose.foundation.layout.Box
6+
import androidx.compose.foundation.layout.Column
7+
import androidx.compose.foundation.layout.PaddingValues
8+
import androidx.compose.foundation.layout.fillMaxSize
9+
import androidx.compose.foundation.layout.fillMaxWidth
10+
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.foundation.lazy.LazyColumn
12+
import androidx.compose.foundation.verticalScroll
13+
import androidx.compose.material.icons.Icons
14+
import androidx.compose.material.icons.filled.MoreVert
15+
import androidx.compose.material.icons.filled.Search
16+
import androidx.compose.material.icons.filled.Star
17+
import androidx.compose.material3.ExperimentalMaterial3Api
18+
import androidx.compose.material3.Icon
19+
import androidx.compose.material3.ListItem
20+
import androidx.compose.material3.ListItemDefaults
21+
import androidx.compose.material3.SearchBar
22+
import androidx.compose.material3.SearchBarDefaults
23+
import androidx.compose.material3.Text
24+
import androidx.compose.runtime.Composable
25+
import androidx.compose.runtime.derivedStateOf
26+
import androidx.compose.runtime.getValue
27+
import androidx.compose.runtime.mutableStateOf
28+
import androidx.compose.runtime.remember
29+
import androidx.compose.runtime.saveable.rememberSaveable
30+
import androidx.compose.runtime.setValue
31+
import androidx.compose.ui.Alignment
32+
import androidx.compose.ui.Modifier
33+
import androidx.compose.ui.graphics.Color
34+
import androidx.compose.ui.semantics.isTraversalGroup
35+
import androidx.compose.ui.semantics.semantics
36+
import androidx.compose.ui.semantics.traversalIndex
37+
import androidx.compose.ui.tooling.preview.Preview
38+
import androidx.compose.ui.unit.dp
39+
import androidx.compose.foundation.rememberScrollState
40+
41+
@OptIn(ExperimentalMaterial3Api::class)
42+
// [START android_compose_components_searchbarbasicfilterlist]
43+
@Composable
44+
fun SearchBarBasicFilterList(modifier: Modifier = Modifier) {
45+
var text by rememberSaveable { mutableStateOf("") }
46+
var expanded by rememberSaveable { mutableStateOf(false) }
47+
Box(
48+
modifier
49+
.fillMaxSize()
50+
.semantics { isTraversalGroup = true })
51+
{
52+
SearchBar(
53+
modifier = Modifier
54+
.align(Alignment.TopCenter)
55+
.semantics { traversalIndex = 0f },
56+
inputField = {
57+
SearchBarDefaults.InputField(
58+
query = text,
59+
onQueryChange = { text = it },
60+
onSearch = { expanded = false },
61+
expanded = expanded,
62+
onExpandedChange = { expanded = it },
63+
placeholder = { Text("Hinted search text") }
64+
)
65+
},
66+
expanded = expanded,
67+
onExpandedChange = { expanded = it },
68+
) {
69+
Column(Modifier.verticalScroll(rememberScrollState())) {
70+
repeat(4) { index ->
71+
val resultText = "Suggestion $index"
72+
ListItem(
73+
headlineContent = { Text(resultText) },
74+
supportingContent = { Text("Additional info") },
75+
modifier = Modifier
76+
.clickable {
77+
text = resultText
78+
expanded = false
79+
}
80+
.fillMaxWidth()
81+
)
82+
}
83+
}
84+
}
85+
}
86+
}
87+
// [END android_compose_components_searchbarbasicfilterlist]
88+
89+
@Preview(showBackground = true)
90+
@Composable
91+
private fun SearchBarBasicFilterListPreview() {
92+
SearchBarBasicFilterList()
93+
}
94+
95+
// [START android_compose_components_searchbarfilterlist]
96+
@OptIn(ExperimentalMaterial3Api::class)
97+
@Composable
98+
fun SearchBarFilterList(
99+
list: List<String>,
100+
modifier: Modifier = Modifier
101+
) {
102+
var text by rememberSaveable { mutableStateOf("") }
103+
val filteredList by remember {
104+
derivedStateOf {
105+
list.filter { it.lowercase().contains(text.lowercase()) }
106+
}
107+
}
108+
var expanded by rememberSaveable { mutableStateOf(false) }
109+
110+
Box(
111+
modifier
112+
.fillMaxSize()
113+
.semantics { isTraversalGroup = true }) {
114+
SearchBar(
115+
modifier = Modifier
116+
.align(Alignment.TopCenter)
117+
.semantics { traversalIndex = 0f },
118+
inputField = {
119+
SearchBarDefaults.InputField(
120+
query = text,
121+
onQueryChange = { text = it },
122+
onSearch = { expanded = false },
123+
expanded = expanded,
124+
onExpandedChange = { expanded = it },
125+
placeholder = { Text("Hinted search text") },
126+
leadingIcon = { Icon(Icons.Default.Search, contentDescription = null) },
127+
trailingIcon = { Icon(Icons.Default.MoreVert, contentDescription = null) },
128+
)
129+
},
130+
expanded = expanded,
131+
onExpandedChange = { expanded = it },
132+
) {
133+
LazyColumn {
134+
items(count = filteredList.size) { index ->
135+
val resultText = filteredList[index]
136+
ListItem(
137+
headlineContent = { Text(resultText) },
138+
supportingContent = { Text("Additional info") },
139+
leadingContent = {
140+
Icon(
141+
Icons.Filled.Star,
142+
contentDescription = null
143+
)
144+
},
145+
colors = ListItemDefaults.colors(containerColor = Color.Transparent),
146+
modifier = Modifier
147+
.clickable {
148+
text = resultText
149+
expanded = false
150+
}
151+
.fillMaxWidth()
152+
.padding(horizontal = 16.dp, vertical = 4.dp)
153+
)
154+
}
155+
}
156+
}
157+
LazyColumn(
158+
contentPadding = PaddingValues(
159+
start = 16.dp,
160+
top = 72.dp,
161+
end = 16.dp,
162+
bottom = 16.dp
163+
),
164+
verticalArrangement = Arrangement.spacedBy(8.dp),
165+
modifier = Modifier.semantics {
166+
traversalIndex = 1f
167+
},
168+
) {
169+
items(count = filteredList.size) {
170+
Text(text = filteredList[it])
171+
}
172+
}
173+
}
174+
}
175+
// [END android_compose_components_searchbarfilterlist]
176+
177+
@Preview(showBackground = true)
178+
@Composable
179+
fun AppSearchBar(modifier: Modifier = Modifier) {
180+
SearchBarFilterList(
181+
list = listOf(
182+
"Cupcake",
183+
"Donut",
184+
"Eclair",
185+
"Froyo",
186+
"Gingerbread",
187+
"Honeycomb",
188+
"Ice Cream Sandwich",
189+
"Jelly Bean",
190+
"KitKat",
191+
"Lollipop",
192+
"Marshmallow",
193+
"Nougat",
194+
"Oreo",
195+
"Pie"
196+
),
197+
modifier
198+
)
199+
}
200+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1" />
34+
</vector>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.shared" >
4+
5+
<uses-sdk android:minSdkVersion="21" />
6+
7+
</manifest>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": 3,
3+
"artifactType": {
4+
"type": "AAPT_FRIENDLY_MERGED_MANIFESTS",
5+
"kind": "Directory"
6+
},
7+
"applicationId": "com.example.shared",
8+
"variantName": "debug",
9+
"elements": [
10+
{
11+
"type": "SINGLE",
12+
"filters": [],
13+
"attributes": [],
14+
"outputFile": "AndroidManifest.xml"
15+
}
16+
],
17+
"elementType": "File"
18+
}
22 Bytes
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
aarFormatVersion=1.0
2+
aarMetadataVersion=1.0
3+
minCompileSdk=1
4+
minCompileSdkExtension=0
5+
minAndroidGradlePluginVersion=1.0.0
6+
coreLibraryDesugaringEnabled=false
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

shared/build/intermediates/annotations_typedef_file/debug/typedefs.txt

Whitespace-only changes.
3.11 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
int color colorAccent 0x0
2+
int color colorPrimary 0x0
3+
int color colorPrimaryDark 0x0
4+
int drawable ic_launcher_background 0x0
5+
int drawable ic_launcher_foreground 0x0
6+
int layout activity_main 0x0
7+
int mipmap ic_launcher 0x0
8+
int mipmap ic_launcher_round 0x0
9+
int string app_name 0x0
10+
int style AppTheme 0x0

0 commit comments

Comments
 (0)