I have a searchfield on the top of the screen (but not in the toolbar) and below I have he recyclerview content:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_fragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#DBDBDB"
android:orientation="vertical">
<SearchView
android:id="@+id/searchView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:background="@drawable/searchfield">
</SearchView>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DBDBDB" />
</RelativeLayout>
and a part of my java code:
setContentView(R.layout.search_layout); //this is the above XML
movieList = new ArrayList<>();
recyclerView = (RecyclerView)findViewById(R.id.recyclerview); // this is the recyclerview XML (thumb, title etc)
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerAdapter = new RecyclerAdapter(this, "Search", movieList);
SearchView search = findViewById(R.id.searchView1);
int id = search.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = search.findViewById(id);
textView.setTextColor(ContextCompat.getColor(this,R.color.colorPrimaryDark));
textView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
int searchPlateId = search.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlateView = search.findViewById(searchPlateId);
if (searchPlateView != null) {searchPlateView.setBackgroundColor(Color.TRANSPARENT);}
search.setQueryHint(getString(R.string.searchfield));
search.onActionViewExpanded();
search.requestFocusFromTouch();
search.setIconifiedByDefault(false);
recyclerView.setAdapter(recyclerAdapter);
and finally when user types at least 3 chars, search results appears from the server link (I am using retrofit):
public boolean onQueryTextChange(String newText) {
if (newText.length() > 2)
{
movieList = new ArrayList<>();
recyclerAdapter.notifyDataSetChanged();
recyclerView.setVisibility(VISIBLE);
GetData(lng, newText);
}
else {recyclerView.setVisibility(INVISIBLE);}
return false;
}
private void GetData(String lng, String newText) {
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<List<Movie>> call = apiService.getSearch(lng, 0, newText);
call.enqueue(new Callback<List<Movie>>() {
@Override
public void onResponse(@NonNull Call<List<Movie>> call, @NonNull Response<List<Movie>> response) {
movieList = response.body();
if (dialog != null) {
dialog.dismiss(); dialog = null;
}
if (response.isSuccessful()) {
recyclerAdapter.setMovieList(movieList);
} else {
showMsgSnack(getString(R.string.Nodata));
}
}
@Override
public void onFailure(@NonNull Call<List<Movie>> call, @NonNull Throwable t) {
if (dialog != null) {
dialog.dismiss(); dialog = null;
}
if(t instanceof UnknownHostException){
showMsgSnack(getString(R.string.Network));
}
else if(t instanceof SocketTimeoutException){
showMsgSnack(getString(R.string.ServerTimeout));
}
else {
showMsgSnack(getString(R.string.ServerError));
}
}
});
}
The search functionality works very well, as it is working on the server side and only correct results are returned.
The problem is, when user enters at least 3 chars, the search field disappears and only results are visible, but the user can't type anywhere as the searchfield is gone. So from the start the field is there, it is gone only when search results come up.
In the past I used ListView and it worked fine, but now I am using recyclerview and recycleradapter everywhere, not sure why is the field hidden. I found many search solutions with recyclerview, that the field was placed in the toolbar, but I don't want it, the searchview needs to be below it.
problem with layout file. in RelativeLayout
needed to use layout_below
as property.