Search code examples
androidandroid-recyclerviewmenusearchviewsearchbar

Search bar not visible to RecyclerView in Fragment


I am using the Search bar in Recycler view on Fragment but it doesn't seem to be please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me. please help me.

menu.xml

<menu 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"
    tools:context=".HomeActivity">

    <item
        android:id="@+id/action_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="@string/search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="always" />

</menu>

here is the java code in fragment and used with fragment

fragment.java

package part.time.job.v2;


import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;
import android.widget.TextView;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

import model.Jobpost;
import ui.JournalRecyclerAdapter;
import ui.JournalRecyclerAdapterEmp;
import util.JournalApi;


/**
 * A simple {@link Fragment} subclass.
 */

    public class ListjobFragment extends Fragment {

        private FirebaseUser user;
        private FirebaseAuth firebaseAuth;
        private FirebaseAuth.AuthStateListener authStateListener;
        private FirebaseFirestore db = FirebaseFirestore.getInstance();
        private List<Jobpost> journalList;
        private RecyclerView recyclerView;
        private Parcelable savedRecyclerLayoutState;
        private JournalRecyclerAdapterEmp journalRecyclerAdapterEmp;

        private CollectionReference collectionReference=db.collection("Employer");
        private TextView noJournalEntry;
        private SearchView searchView = null;
        private SearchView.OnQueryTextListener queryTextListener;

        public ListjobFragment() {
            // Required empty public constructor
        }




        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);

            // Inflate the layout for this fragment
            View view=inflater.inflate(R.layout.fragment_listjob, container, false);

            firebaseAuth= FirebaseAuth.getInstance();
            user =firebaseAuth.getCurrentUser();

            noJournalEntry= view.findViewById(R.id.no_list);
            journalList=new ArrayList<>();

            recyclerView=view.findViewById(R.id.recyclerView);
           recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            return view;
        }




        @Override
        public void onStart() {
            super.onStart();
    //        user= firebaseAuth.getCurrentUser();
    //        firebaseAuth.addAuthStateListener(authStateListener);
            if (journalList!=null){
                journalList.clear();
            }

            collectionReference.document(JournalApi.getInstance().getUserId()).collection("Job Post").whereEqualTo("userId",JournalApi.getInstance().getUserId())
                    .get()
                    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                            if (!queryDocumentSnapshots.isEmpty()){
                                for (QueryDocumentSnapshot journals: queryDocumentSnapshots){
    //                                Journal journal=journals.toObject(Journal.class);
                                    Jobpost jobpost=journals.toObject(Jobpost.class);
                                    journalList.add(jobpost);

                                }

                                journalRecyclerAdapterEmp = new JournalRecyclerAdapterEmp(getActivity(),journalList);
                                recyclerView.setAdapter(journalRecyclerAdapterEmp);
                                journalRecyclerAdapterEmp.notifyDataSetChanged();

                            }else {

                                noJournalEntry.setVisibility(View.VISIBLE);

                            }
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {

                        }
                    });

        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.menu, menu);
            MenuItem searchItem = menu.findItem(R.id.action_search);
            SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);

            if (searchItem != null) {
                searchView = (SearchView) searchItem.getActionView();
            }
            if (searchView != null) {
                searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

                queryTextListener = new SearchView.OnQueryTextListener() {
                    @Override
                    public boolean onQueryTextChange(String newText) {
                        Log.i("onQueryTextChange", newText);

                        return true;
                    }
                    @Override
                    public boolean onQueryTextSubmit(String query) {
                        Log.i("onQueryTextSubmit", query);

                        return true;
                    }
                };
                searchView.setOnQueryTextListener(queryTextListener);
            }
            super.onCreateOptionsMenu(menu, inflater);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_search:
                    // Not implemented here
                    return false;
                default:
                    break;
            }
            searchView.setOnQueryTextListener(queryTextListener);
            return super.onOptionsItemSelected(item);
        }



    }

Solution

  • Put this in your code and you'll get your searchBar:

    @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
        }
    

    Basically in fragment, to display the menu items we've to use "setHasOptionsMenu".