I'm currently facing a problem with a search bar in my app. The search bar is part of an inflated layout from a menu. Within this menu, there's an icon called 'log_out_button' that should appear in the search bar when it's clicked. However, this icon only shows up momentarily upon clicking the search bar.
I've checked the live-edit view, and everything seems to be fine:
However, when I actually start the app, the issue becomes apparent: here's a video demonstration.
Below is the relevant code snippets:
fragment_note_listing.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.search.SearchBar
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/search_your_notes"
app:backgroundTint="?attr/colorBackgroundFloating"
app:menu="@menu/menu"
app:navigationIcon="@drawable/ic_menu" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_of_notes"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginVertical="16dp"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/app_bar" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginBottom="24dp"
android:clickable="true"
android:contentDescription="@string/add_note_button"
android:focusable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_add" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.search.SearchView
android:id="@+id/search_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/search_your_notes"
android:inputType="text"
android:maxLines="1"
app:backgroundTint="?attr/colorBackgroundFloating"
app:layout_anchor="@id/search_bar">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="?android:attr/colorBackground" />
</com.google.android.material.search.SearchView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/log_out_button"
android:contentDescription="@string/log_out_button"
android:title="@string/log_out_button"
android:icon="@drawable/ic_log_out"
app:showAsAction="always" />
</menu>
Function onCreateView in NoteListingFragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = FragmentNoteListingBinding.inflate(layoutInflater)
(requireActivity() as AppCompatActivity).setSupportActionBar(binding.searchBar)
binding.searchBar.inflateMenu(R.menu.menu)
binding.searchBar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.log_out_button -> {
authenticationViewModel.logout()
findNavController().navigate(R.id.action_noteListingFragment_to_welcomeFragment)
true
}
else -> false
}
}
return binding.root
}
There is menu layout
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/log_out_button"
android:contentDescription="@string/log_out_button"
android:title="@string/log_out_button"
android:icon="@drawable/ic_log_out"
app:showAsAction="always" />
</menu>
There is another solution to fix this issue. The solution is removing this code snippet:
(requireActivity() as AppCompatActivity).setSupportActionBar(binding.searchBar)
To add a TextChangedListener to the SearchView, you can use the editText property of the SearchView and add the TextChangedListener as follows:
binding.searchView.editText.addTextChangedListener {
// your code
}