1

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. enter image description here 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.

nandu
  • 123
  • 1
  • 9
  • Do the menu options change at all as you go to the different fragments? I never saw anything in Main activity of setting the toolbar as the supportActionBar – Nick Mowen Jul 22 '18 at 13:51
  • It's only search, no other menu options. – nandu Jul 22 '18 at 13:53
  • Gotcha, then it definitely sounds like you should add a setSupportActionBar(toolbar) to your mainactivity and that will make the options menu code run – Nick Mowen Jul 22 '18 at 13:54
  • Okay, let me try tgat and see. Is everything else as it should be? – nandu Jul 22 '18 at 14:11
  • Could you give bit more detail? I tried it onCreate method, but setSupportActionBar() doesn't take any arguments. Also, toolbar is in fragment. – nandu Jul 22 '18 at 16:16
  • setSupportActionBar should take the argument as a toolbar. So what you should do is inside your fragment call ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar) – Nick Mowen Jul 22 '18 at 18:30
  • The problem was with wrong import and it's working!!!! But there are three search button now and it disappears after I click on it to type! – nandu Jul 23 '18 at 04:46
  • If you add it as an answer, I can accept it. – nandu Jul 24 '18 at 10:14

1 Answers1

1

setSupportActionBar should take the argument as a toolbar. So what you should do is, inside your fragment call

((AppCompatActivity) getActivity()).setSupportActionBar(toolbar)

Nick Mowen
  • 2,572
  • 2
  • 22
  • 38