I must have read almost all the question and answers related to this, but still cannot get it to work. Below is the rough sketch of how the app looks.
Adding code below to give structure of the layout and where I might be doing wrong.
activity_main.xml
<android.support.constraint.ConstraintLayout >
<android.support.design.widget.BottomNavigationView>
</android.support.design.widget.BottomNavigationView>
<FrameLayout>
</FrameLayout>
bottom_nav_fragment.xml
<android.support.design.widget.CoordinatorLayout >
<android.support.design.widget.AppBarLayout
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/liquor_type_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:tabMode="scrollable">
<android.support.design.widget.TabItem
/>
<android.support.design.widget.TabItem
/>
<android.support.design.widget.TabItem
/>
<android.support.design.widget.TabItem
/>
<android.support.design.widget.TabItem
/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/price_list_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
For each of tab item, I have used a common fragment which will have different data in recyclerView.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.nandu.oru10ml.price_list.CommonPriceFragment">
<android.support.v7.widget.RecyclerView >
</android.support.v7.widget.RecyclerView>
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// MenuInflater inflater = getS
Log.d("Activity Options", "inside onCreateOptionsMenu ");
getMenuInflater().inflate(R.menu.search_item, menu);
return true;
}
BottomNavFragment.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Fragment onCreate", "inside onCreate ");
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Fragment Options", "inside onCreateOptionsMenu ");
inflater.inflate(R.menu.search_item, menu);
super.onCreateOptionsMenu(menu,inflater);
}
CommonFragment.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("Fragment onCreate", "inside onCreate ");
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d("Fragment Options", "inside onCreateOptionsMenu ");
inflater.inflate(R.menu.search_item, menu);
super.onCreateOptionsMenu(menu,inflater);
}
and finally, search_item.xml
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/action_search"
android:title="Search for Brand"
android:icon="@drawable/ic_search_black_24dp"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView">
</item>
</menu>
The reason why it is not working would also help me understand the root cause.
This SearhView is applicable to 2 of the bottom nav Fragment and all of the nested Fragments inside, to search Recyclerview present in each of it.
setSupportActionBar should take the argument as a toolbar. So what you should do is, inside your fragment call
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar)