-
Notifications
You must be signed in to change notification settings - Fork 237
Adding SearchBar examples #408
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
63b82c2
Adding SearchBar examples
jakeroseman b4619e3
Apply Spotless
jakeroseman a3de454
Merge branch 'main' into searchbar
riggaroo 155ce25
Add search bar examples to top compose examples
jakeroseman f30f197
Add content descriptions
jakeroseman 44983b2
Delete shared/build/generated/res/pngs/debug/drawable-anydpi-v24/ic_l…
jakeroseman 4f6318e
remove errant build files from branch
jakeroseman 87326be
handle merge
jakeroseman 4d7e4ab
Apply Spotless
jakeroseman 3ed3118
Merge branch 'main' into searchbar
jakeroseman a7e7645
Merge branch 'main' into searchbar
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
244 changes: 244 additions & 0 deletions
244
compose/snippets/src/main/java/com/example/compose/snippets/components/SearchBar.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,244 @@ | ||
/* | ||
* 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.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.rememberScrollState | ||
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.Button | ||
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 | ||
|
||
@Preview | ||
@Composable | ||
fun SearchBarExamples() { | ||
Column( | ||
modifier = Modifier | ||
.padding(16.dp) | ||
.fillMaxSize(), | ||
verticalArrangement = Arrangement.spacedBy(24.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
var currentExample by remember { mutableStateOf<String?>(null) } | ||
|
||
when (currentExample) { | ||
"basic" -> SearchBarBasicFilterList() | ||
"advanced" -> AppSearchBar() | ||
else -> { | ||
Button(onClick = { currentExample = "basic" }) { | ||
Text("Basic search bar with filter") | ||
} | ||
Button(onClick = { currentExample = "advanced" }) { | ||
Text("Advanced search bar with filter") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@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 = "Search") }, | ||
trailingIcon = { Icon(Icons.Default.MoreVert, contentDescription = "More options") }, | ||
) | ||
}, | ||
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 = "Starred item" | ||
) | ||
}, | ||
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 | ||
) | ||
} |
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
Oops, something went wrong.
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.