view_toolbar_search: Add button to clear searchView's text

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-06-12 16:33:04 +05:30
parent 38f8ccacee
commit 1bc396598e
3 changed files with 28 additions and 1 deletions

View File

@@ -83,6 +83,10 @@ class SearchResultsFragment : BaseFragment(R.layout.fragment_search_result),
imgActionSecondary.setOnClickListener { imgActionSecondary.setOnClickListener {
findNavController().navigate(R.id.downloadFragment) findNavController().navigate(R.id.downloadFragment)
} }
clearButton.apply {
visibility = if (query.isNullOrBlank()) View.GONE else View.VISIBLE
setOnClickListener { searchView.text?.clear() }
}
} }
// Search // Search
@@ -209,7 +213,11 @@ class SearchResultsFragment : BaseFragment(R.layout.fragment_search_result),
} }
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (s.isNotEmpty()) {
binding.layoutViewToolbar.clearButton.visibility = View.VISIBLE
} else {
binding.layoutViewToolbar.clearButton.visibility = View.GONE
}
} }
override fun afterTextChanged(s: Editable) {} override fun afterTextChanged(s: Editable) {}

View File

@@ -65,6 +65,10 @@ class SearchSuggestionFragment : Fragment(R.layout.fragment_search_suggestion) {
imgActionSecondary.setOnClickListener { imgActionSecondary.setOnClickListener {
findNavController().navigate(R.id.downloadFragment) findNavController().navigate(R.id.downloadFragment)
} }
clearButton.apply {
visibility = if (query.isBlank()) View.GONE else View.VISIBLE
setOnClickListener { searchView.text?.clear() }
}
} }
viewModel.liveSearchSuggestions.observe(viewLifecycleOwner) { viewModel.liveSearchSuggestions.observe(viewLifecycleOwner) {
@@ -115,6 +119,9 @@ class SearchSuggestionFragment : Fragment(R.layout.fragment_search_suggestion) {
if (query.isNotEmpty()) { if (query.isNotEmpty()) {
viewModel.observeStreamBundles(query) viewModel.observeStreamBundles(query)
} }
binding.layoutToolbarSearch.clearButton.visibility = View.VISIBLE
} else {
binding.layoutToolbarSearch.clearButton.visibility = View.GONE
} }
} }

View File

@@ -51,6 +51,18 @@
android:paddingEnd="@dimen/padding_normal" android:paddingEnd="@dimen/padding_normal"
android:singleLine="true" /> android:singleLine="true" />
<com.google.android.material.button.MaterialButton
android:id="@+id/clearButton"
style="@style/Widget.Material3.Button.IconButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toStartOf="@id/img_action_secondary"
android:contentDescription="@string/details_changelog"
android:visibility="gone"
app:icon="@drawable/ic_cancel"
app:iconTint="?attr/colorControlNormal" />
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/img_action_secondary" android:id="@+id/img_action_secondary"
android:layout_width="@dimen/icon_size_default" android:layout_width="@dimen/icon_size_default"