Skip to content

Commit

Permalink
Adding SearchBar examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeroseman committed Dec 4, 2024
1 parent 0d07b08 commit 63b82c2
Show file tree
Hide file tree
Showing 41 changed files with 630 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package com.example.compose.snippets.components

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Star
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.SearchBar
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
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.graphics.Color
import androidx.compose.ui.semantics.isTraversalGroup
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.traversalIndex
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.rememberScrollState

@OptIn(ExperimentalMaterial3Api::class)
// [START android_compose_components_searchbarbasicfilterlist]
@Composable
fun SearchBarBasicFilterList(modifier: Modifier = Modifier) {
var text by rememberSaveable { mutableStateOf("") }
var expanded by rememberSaveable { mutableStateOf(false) }
Box(
modifier
.fillMaxSize()
.semantics { isTraversalGroup = true })
{
SearchBar(
modifier = Modifier
.align(Alignment.TopCenter)
.semantics { traversalIndex = 0f },
inputField = {
SearchBarDefaults.InputField(
query = text,
onQueryChange = { text = it },
onSearch = { expanded = false },
expanded = expanded,
onExpandedChange = { expanded = it },
placeholder = { Text("Hinted search text") }
)
},
expanded = expanded,
onExpandedChange = { expanded = it },
) {
Column(Modifier.verticalScroll(rememberScrollState())) {
repeat(4) { index ->
val resultText = "Suggestion $index"
ListItem(
headlineContent = { Text(resultText) },
supportingContent = { Text("Additional info") },
modifier = Modifier
.clickable {
text = resultText
expanded = false
}
.fillMaxWidth()
)
}
}
}
}
}
// [END android_compose_components_searchbarbasicfilterlist]

@Preview(showBackground = true)
@Composable
private fun SearchBarBasicFilterListPreview() {
SearchBarBasicFilterList()
}

// [START android_compose_components_searchbarfilterlist]
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SearchBarFilterList(
list: List<String>,
modifier: Modifier = Modifier
) {
var text by rememberSaveable { mutableStateOf("") }
val filteredList by remember {
derivedStateOf {
list.filter { it.lowercase().contains(text.lowercase()) }
}
}
var expanded by rememberSaveable { mutableStateOf(false) }

Box(
modifier
.fillMaxSize()
.semantics { isTraversalGroup = true }) {
SearchBar(
modifier = Modifier
.align(Alignment.TopCenter)
.semantics { traversalIndex = 0f },
inputField = {
SearchBarDefaults.InputField(
query = text,
onQueryChange = { text = it },
onSearch = { expanded = false },
expanded = expanded,
onExpandedChange = { expanded = it },
placeholder = { Text("Hinted search text") },
leadingIcon = { Icon(Icons.Default.Search, contentDescription = null) },
trailingIcon = { Icon(Icons.Default.MoreVert, contentDescription = null) },
)
},
expanded = expanded,
onExpandedChange = { expanded = it },
) {
LazyColumn {
items(count = filteredList.size) { index ->
val resultText = filteredList[index]
ListItem(
headlineContent = { Text(resultText) },
supportingContent = { Text("Additional info") },
leadingContent = {
Icon(
Icons.Filled.Star,
contentDescription = null
)
},
colors = ListItemDefaults.colors(containerColor = Color.Transparent),
modifier = Modifier
.clickable {
text = resultText
expanded = false
}
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 4.dp)
)
}
}
}
LazyColumn(
contentPadding = PaddingValues(
start = 16.dp,
top = 72.dp,
end = 16.dp,
bottom = 16.dp
),
verticalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.semantics {
traversalIndex = 1f
},
) {
items(count = filteredList.size) {
Text(text = filteredList[it])
}
}
}
}
// [END android_compose_components_searchbarfilterlist]

@Preview(showBackground = true)
@Composable
fun AppSearchBar(modifier: Modifier = Modifier) {
SearchBarFilterList(
list = listOf(
"Cupcake",
"Donut",
"Eclair",
"Froyo",
"Gingerbread",
"Honeycomb",
"Ice Cream Sandwich",
"Jelly Bean",
"KitKat",
"Lollipop",
"Marshmallow",
"Nougat",
"Oreo",
"Pie"
),
modifier
)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillType="evenOdd"
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"
android:strokeColor="#00000000"
android:strokeWidth="1">
<aapt:attr name="android:fillColor">
<gradient
android:endX="78.5885"
android:endY="90.9159"
android:startX="48.7653"
android:startY="61.0927"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
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"
android:strokeColor="#00000000"
android:strokeWidth="1" />
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shared" >

<uses-sdk android:minSdkVersion="21" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 3,
"artifactType": {
"type": "AAPT_FRIENDLY_MERGED_MANIFESTS",
"kind": "Directory"
},
"applicationId": "com.example.shared",
"variantName": "debug",
"elements": [
{
"type": "SINGLE",
"filters": [],
"attributes": [],
"outputFile": "AndroidManifest.xml"
}
],
"elementType": "File"
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
aarFormatVersion=1.0
aarMetadataVersion=1.0
minCompileSdk=1
minCompileSdkExtension=0
minAndroidGradlePluginVersion=1.0.0
coreLibraryDesugaringEnabled=false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Binary file not shown.
10 changes: 10 additions & 0 deletions shared/build/intermediates/compile_symbol_list/debug/R.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
int color colorAccent 0x0
int color colorPrimary 0x0
int color colorPrimaryDark 0x0
int drawable ic_launcher_background 0x0
int drawable ic_launcher_foreground 0x0
int layout activity_main 0x0
int mipmap ic_launcher 0x0
int mipmap ic_launcher_round 0x0
int string app_name 0x0
int style AppTheme 0x0
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#Wed Jul 31 14:04:11 BST 2024
com.example.shared-main-6\:/drawable/ic_launcher_background.xml=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/drawable/ic_launcher_background.xml
com.example.shared-main-6\:/layout/activity_main.xml=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/layout/activity_main.xml
com.example.shared-main-6\:/mipmap-anydpi-v26/ic_launcher.xml=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-anydpi-v26/ic_launcher.xml
com.example.shared-main-6\:/mipmap-anydpi-v26/ic_launcher_round.xml=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-anydpi-v26/ic_launcher_round.xml
com.example.shared-main-6\:/mipmap-hdpi/ic_launcher.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-hdpi-v4/ic_launcher.png
com.example.shared-main-6\:/mipmap-hdpi/ic_launcher_round.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-hdpi-v4/ic_launcher_round.png
com.example.shared-main-6\:/mipmap-mdpi/ic_launcher.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-mdpi-v4/ic_launcher.png
com.example.shared-main-6\:/mipmap-mdpi/ic_launcher_round.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-mdpi-v4/ic_launcher_round.png
com.example.shared-main-6\:/mipmap-xhdpi/ic_launcher.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-xhdpi-v4/ic_launcher.png
com.example.shared-main-6\:/mipmap-xhdpi/ic_launcher_round.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-xhdpi-v4/ic_launcher_round.png
com.example.shared-main-6\:/mipmap-xxhdpi/ic_launcher.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-xxhdpi-v4/ic_launcher.png
com.example.shared-main-6\:/mipmap-xxhdpi/ic_launcher_round.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-xxhdpi-v4/ic_launcher_round.png
com.example.shared-main-6\:/mipmap-xxxhdpi/ic_launcher.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-xxxhdpi-v4/ic_launcher.png
com.example.shared-main-6\:/mipmap-xxxhdpi/ic_launcher_round.png=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/mipmap-xxxhdpi-v4/ic_launcher_round.png
com.example.shared-pngs-0\:/drawable-anydpi-v24/ic_launcher_foreground.xml=/Users/jakeroseman/snippets-2024/snippets/shared/build/intermediates/packaged_res/debug/drawable-anydpi-v24/ic_launcher_foreground.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorAccent">#D81B60</color>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<string name="app_name">snippets</string>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<merger version="3"><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="main$Generated" generated="true" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res"><file path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/drawable-v24/ic_launcher_foreground.xml" preprocessing="true" qualifiers="v24"><generated-file path="/Users/jakeroseman/snippets-2024/snippets/shared/build/generated/res/pngs/debug/drawable-anydpi-v24/ic_launcher_foreground.xml" qualifiers="anydpi-v24" type="drawable"/></file></source></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="main" generated-set="main$Generated" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res"><file name="ic_launcher" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-mdpi/ic_launcher.png" qualifiers="mdpi-v4" type="mipmap"/><file name="ic_launcher_round" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-mdpi/ic_launcher_round.png" qualifiers="mdpi-v4" type="mipmap"/><file name="ic_launcher" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-hdpi/ic_launcher.png" qualifiers="hdpi-v4" type="mipmap"/><file name="ic_launcher_round" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-hdpi/ic_launcher_round.png" qualifiers="hdpi-v4" type="mipmap"/><file name="ic_launcher_background" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/drawable/ic_launcher_background.xml" qualifiers="" type="drawable"/><file name="ic_launcher" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-xxxhdpi/ic_launcher.png" qualifiers="xxxhdpi-v4" type="mipmap"/><file name="ic_launcher_round" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png" qualifiers="xxxhdpi-v4" type="mipmap"/><file name="activity_main" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/layout/activity_main.xml" qualifiers="" type="layout"/><file name="ic_launcher" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-xxhdpi/ic_launcher.png" qualifiers="xxhdpi-v4" type="mipmap"/><file name="ic_launcher_round" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-xxhdpi/ic_launcher_round.png" qualifiers="xxhdpi-v4" type="mipmap"/><file path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/values/colors.xml" qualifiers=""><color name="colorPrimary">#008577</color><color name="colorPrimaryDark">#00574B</color><color name="colorAccent">#D81B60</color></file><file path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/values/styles.xml" qualifiers=""><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style></file><file path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/values/strings.xml" qualifiers=""><string name="app_name">snippets</string></file><file name="ic_launcher" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-xhdpi/ic_launcher.png" qualifiers="xhdpi-v4" type="mipmap"/><file name="ic_launcher_round" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-xhdpi/ic_launcher_round.png" qualifiers="xhdpi-v4" type="mipmap"/><file name="ic_launcher" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" qualifiers="anydpi-v26" type="mipmap"/><file name="ic_launcher_round" path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" qualifiers="anydpi-v26" type="mipmap"/></source></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="debug$Generated" generated="true" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/jakeroseman/snippets-2024/snippets/shared/src/debug/res"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="debug" generated-set="debug$Generated" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/jakeroseman/snippets-2024/snippets/shared/src/debug/res"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="generated$Generated" generated="true" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/jakeroseman/snippets-2024/snippets/shared/build/generated/res/resValues/debug"/></dataSet><dataSet aapt-namespace="http://schemas.android.com/apk/res-auto" config="generated" generated-set="generated$Generated" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/jakeroseman/snippets-2024/snippets/shared/build/generated/res/resValues/debug"/></dataSet><mergedItems/></merger>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<merger version="3"><dataSet config="main" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/jakeroseman/snippets-2024/snippets/shared/src/main/jniLibs"/></dataSet><dataSet config="debug" ignore_pattern="!.svn:!.git:!.ds_store:!*.scc:.*:&lt;dir>_*:!CVS:!thumbs.db:!picasa.ini:!*~"><source path="/Users/jakeroseman/snippets-2024/snippets/shared/src/debug/jniLibs"/></dataSet></merger>
Loading

0 comments on commit 63b82c2

Please sign in to comment.