How to add a SearchView in a fragment on Android?

0

I created a Fragment and I'm trying to add a SearchView to this Fragment , but to no avail, I found no consistent example on the Internet, and Google documentation only implements SearchView / em>. Here 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()));
}

}

search_field.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 I have not succeeded in any. Could you help?

Att.

    
asked by anonymous 20.04.2017 / 03:36

3 answers

0

According to documentation missing return true; in method < strong> 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;
}
    
20.04.2017 / 17:47
0

I have a similar problem and would like to know if anyone could help me. I created the XML below and, as below, I placed within the Toolbar a SearchView. But I can not 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 take Null Point error as if the object did not exist in View (Fragment):

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

Below is the entire 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>

Can anyone help me with how to instantiate and implement search action using this SearchView?

Thank you.

    
09.05.2017 / 13:38
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 menu_tab1_fix.xml 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>
    
14.01.2018 / 15:25