Please use:
- com.google.android.material.search.SearchBar
- com.google.android.material.search.SearchView
- Search component for Android
- Material You Design
- Styling
- Kotlin
- Java = 1.8
- Kotlin = 1.8
Add the dependency to your gradle file:
repositories {
google()
mavenCentral()
}
dependencies {
implementation 'io.github.lapism:search:2.0.1'
}binding.materialSearchView.requestFocus()
binding.materialSearchView.clearFocus()valtoolbar = binding.materialSearchBar.getToolbar()
setSupportActionBar(toolbar)
binding.materialSearchBar.apply {
navigationIconCompat = NavigationIconCompat.SEARCHsetHint(getString(R.string.search))
setOnClickListener {
binding.materialSearchView.requestFocus()
}
setNavigationOnClickListener {
binding.materialSearchView.requestFocus()
}
}binding.materialSearchView.apply {
addView(recyclerView)
navigationIconCompat = NavigationIconCompat.ARROWsetNavigationOnClickListener {
binding.materialSearchView.clearFocus()
}
setHint(getString(R.string.search))
setOnQueryTextListener(object : MaterialSearchView.OnQueryTextListener {
overridefunonQueryTextChange(newText: CharSequence) {
adapter.filter(newText)
}
overridefunonQueryTextSubmit(query: CharSequence) {
}
})
setOnFocusChangeListener(object : MaterialSearchView.OnFocusChangeListener {
overridefunonFocusChange(hasFocus: Boolean) {
}
})
}You have to use app theme Theme.Material3.* or Theme.MaterialComponents.*.
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="false"tools:context=".activity.MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"android:layout_height="wrap_content">
<!-- Simple MaterialToolbar extension -->
<com.lapism.search.widget.MaterialSearchBar
android:id="@+id/material_search_bar"android:layout_width="match_parent"android:layout_height="wrap_content"app:layout_scrollFlags="scroll|enterAlways|snap" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:layout_behavior="@string/material_search_bar_scrolling_view_behavior"app:navGraph="@navigation/mobile_navigation" />
<com.lapism.search.widget.MaterialSearchView
android:id="@+id/material_search_view"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_anchor="@id/material_search_bar" />
<BottomNavigationViewandroid:id="@+id/bottom_navigation_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:orientation="vertical"app:layout_insetEdge="bottom"app:menu="@menu/menu_bottom_nav" />
</androidx.coordinatorlayout.widget.CoordinatorLayout><declare-styleablename="MaterialSearchBar">
<attrname="search_navigationIconCompat"format="enum">
<enumname="none"value="0" />
<enumname="arrow"value="1" />
<enumname="search"value="2" />
</attr>
<attrname="search_navigationIcon"format="reference" />
<attrname="search_navigationContentDescription"format="reference" />
<attrname="search_navigationBackgroundColor"format="reference" />
<attrname="search_navigationElevation"format="dimension" />
<attrname="search_radius"format="dimension" />
<attrname="android:hint" />
<attrname="android:layout_marginStart" />
<attrname="android:layout_marginEnd" />
<attrname="android:layout_marginTop" />
<attrname="android:layout_marginBottom" />
</declare-styleable>
<declare-styleablename="MaterialSearchView">
<attrname="search_navigationIconCompat" />
<attrname="search_navigationIcon" />
<attrname="search_navigationContentDescription" />
<attrname="search_navigationBackgroundColor" />
<attrname="search_navigationElevation" />
<attrname="search_clearIcon"format="reference" />
<attrname="search_dividerColor"format="reference" />
<attrname="search_scrimColor"format="reference" />
<attrname="android:hint" />
<attrname="android:imeOptions" />
<attrname="android:inputType" />
</declare-styleable>Animation
- animation like Google, needs help :)
- Martin Lapiš - GitHub
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
