I am making app using API and fetched all the data perfectly. I also have search bar on recycler view. Now I want to save the search history which user searches on the app. I have followed the solutions from stackoverflow but I am not able to save the history.
Search history in Android quick search
I want to know that where I have to make searchable.xml
? Also I am using edittext
for performing search in my code so I have remove edittext
and replace with searchable
?
I little confused in using and setting up the content provider
.
My xml
file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".ui.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="gone"/>
<TextView
android:id="@+id/txt_error_popular"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/connection_problem"
android:visibility="gone"/>
<EditText
android:id="@+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:inputType="text"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:hint="@string/search_for_something"
android:layout_marginBottom="4dp"
android:importantForAutofill="no" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_photos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:scrollbars="vertical">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
My main activity
where I am performing search:
search(DEFAULT_SEARCH)
search_box?.setOnEditorActionListener(object : TextView.OnEditorActionListener{
override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
if (actionId == EditorInfo.IME_ACTION_SEARCH){
val searchTerm = search_box.text.trim()
if (searchTerm.isNotEmpty()){
rv_photos?.scrollToPosition(0)
photoAdapter.submitList(null)
search(searchTerm.toString())
currentFocus?.let {
val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(it.windowToken, HIDE_NOT_ALWAYS)
}
}
return true
}
return false
}
})
}
private fun search(searchTerm: String){
viewModel.performSearch(searchTerm)
}
Now I just want to save the recent search?