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.
Have you seen this solution? http://blog.aimanbaharum.com/2015/01/29/android-development-8-implementing-searchview-within-a-fragment/
– Rene Freak