How to add a Searchview to an Android Fragment?

Asked

Viewed 803 times

0

I created a Fragment and I’m trying to add a SearchView in that Fragment, but unsuccessfully, I haven’t found any consistent examples on the Internet and Google documentation provides only implementation of SearchView in Activities. Below is the code: Fragment:

public class BuscarFragment extends Fragment {

public BuscarFragment() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_buscar , container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.campo_pesquisa, menu);

    SearchManager searchManager = (SearchManager)getContext().getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.barra_pesquisa).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
}

}

campo_search.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/barra_pesquisa"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Buscar"
        app:icon="@drawable/ic_search"
        app:showAsAction="ifRoom"
        />
</menu>

I have tried several solutions and have not succeeded in any. They could help?

Att.

  • Have you seen this solution? http://blog.aimanbaharum.com/2015/01/29/android-development-8-implementing-searchview-within-a-fragment/

3 answers

0

I have a similar problem, and I was wondering if anyone could help me. I created the XML below and, as below, I put inside the Toolbar a Searchview. However I can’t instantiate it in the Fragment code.

 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">

            <SearchView
                android:id="@+id/xProcurar"
                android:background="@color/branco"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:elevation="5dp"
                android:fitsSystemWindows="true"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:clickable="true"/>

        </android.support.v7.widget.Toolbar>

When I try to instantiate this Searchview, as below, I make Null Point error as if the object did not exist in the View (Fragment) :

SearchView sv = (SearchView) view.findViewById(R.id.xProcurar);

Below follows all Fragment XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">

<android.support.design.widget.AppBarLayout
    android:id="@+id/xToolBar"
    android:layout_width="match_parent"
    android:layout_height="280dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/xBgImagem"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg1"
            android:scaleType="centerCrop"
            app:layout_collapseMode="pin"/>


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">

            <SearchView
                android:background="@color/branco"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:elevation="5dp"
                android:fitsSystemWindows="true"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:clickable="true"/>

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>


<RelativeLayout
    app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/xScrollViewBotoes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">

        <RelativeLayout
            android:id="@+id/xLayoutBotoes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/divisao1"
                android:background="@drawable/divisao_branca"
                android:layout_width="match_parent"
                android:layout_height="1dp" />


            <RelativeLayout
                android:layout_below="@+id/divisao1"
                android:id="@+id/layout_btn_cabelo"
                android:layout_width="match_parent"
                android:layout_height="150dp">


                <ImageButton
                    android:id="@+id/btn_cabelo"
                    android:background="@drawable/cabelo"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent" />

                <TextView
                    android:id="@+id/txt_btn_cabelo"
                    android:clickable="true"
                    android:text="BARBA/CABELO"
                    android:textColor="@color/branco"
                    android:textSize="15dp"
                    android:gravity="center|center_vertical"
                    android:layout_alignTop="@+id/btn_cabelo"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>

            </RelativeLayout>

            <ImageView
                android:id="@+id/divisao2"
                android:layout_below="@+id/layout_btn_cabelo"
                android:background="@drawable/divisao_branca"
                android:layout_width="match_parent"
                android:layout_height="1dp" />

            <RelativeLayout
                android:layout_below="@+id/divisao2"
                android:id="@+id/layout_btn_maquiagem"
                android:layout_width="match_parent"
                android:layout_height="150dp">

                <ImageButton
                    android:id="@+id/btn_maquiagem"
                    android:background="@drawable/maquiagem"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"/>

                <TextView
                    android:id="@+id/txt_btn_maquiagem"
                    android:clickable="true"
                    android:text="MAQUIAGEM"
                    android:textColor="@color/branco"
                    android:textSize="15dp"
                    android:gravity="center|center_vertical"
                    android:layout_alignTop="@+id/btn_maquiagem"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </RelativeLayout>


            <ImageView
                android:id="@+id/divisao3"
                android:layout_below="@+id/layout_btn_maquiagem"
                android:background="@drawable/divisao_branca"
                android:layout_width="match_parent"
                android:layout_height="1dp" />

            <RelativeLayout
                android:layout_below="@+id/divisao3"
                android:id="@+id/layout_btn_manicure"
                android:layout_width="match_parent"
                android:layout_height="150dp">

                <ImageButton
                    android:id="@+id/btn_manicure"
                    android:background="@drawable/unha"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"/>

                <TextView
                    android:id="@+id/txt_btn_manicure"
                    android:clickable="true"
                    android:text="PÉ E MÃO"
                    android:textColor="@color/branco"
                    android:textSize="15dp"
                    android:gravity="center|center_vertical"
                    android:layout_alignTop="@+id/btn_manicure"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </RelativeLayout>

            <ImageView
                android:id="@+id/divisao4"
                android:layout_below="@+id/layout_btn_manicure"
                android:background="@drawable/divisao_branca"
                android:layout_width="match_parent"
                android:layout_height="1dp" />

            <RelativeLayout
                android:layout_below="@+id/divisao4"
                android:id="@+id/layout_btn_depilacao"
                android:layout_width="match_parent"
                android:layout_height="150dp">

                <ImageButton
                    android:id="@+id/btn_depilacao"
                    android:background="@drawable/depilacao"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"/>

                <TextView
                    android:id="@+id/txt_btn_depilacao"
                    android:clickable="true"
                    android:text="DEPILAÇÃO"
                    android:textColor="@color/branco"
                    android:textSize="15dp"
                    android:gravity="center|center_vertical"
                    android:layout_alignTop="@+id/btn_depilacao"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
            </RelativeLayout>

        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>
</RelativeLayout>

Someone could help me on how to instantiate and implement search action using this Searchview?

Thank you.

0

In Fragment

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_tab1_fixo, menu);
    final MenuItem item = menu.findItem(R.id.search_fixo);
 }

The fixed.xml menu_tab1_from the Fragment menu

<?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/search_fixo"
    android:icon="@android:drawable/ic_menu_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always|collapseActionView" />
</menu>

0


According to the documentation missed Return true; in the method onCreateOptionsMenu():

public boolean onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.campo_pesquisa, menu);

    SearchManager searchManager = (SearchManager)getContext().getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.barra_pesquisa).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
    return true;
}
  • The onCreateOptionsMenu method of my app is in a class that extends from Fragment, it is void type. The Boolean method is in Mainactivity.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.