title: Hide AppBarLayout using toolbar in fragment in Android
body: <p>This is my manifest</p>
  
  <pre><code>&lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
  </code></pre>
  
  <p>This is my some part of MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  FragmentManager fragmentManager;
  FragmentTransaction fragmentTransaction;
  Fragment fragment,recepie_fragment,products_fragment,swadeshi_fragment,previous;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
     // this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_yash);
      //setSupportActionBar(toolbar);
      //setSupportActionBar(null);
    //  final View ab=(View)findViewById(R.id.appbarmain);
  
      fragmentManager = getSupportFragmentManager();
      fragmentTransaction=fragmentManager.beginTransaction();
  
      recepie_fragment=new Recepie_Fragment();
  
      products_fragment=new Products_Fragment();
      swadeshi_fragment=new Swadeshi_Fragment();
      fragment=recepie_fragment;//new Fragment();
      fragmentTransaction.replace(R.id.bglayout,fragment);
      fragmentTransaction.commit();
      previous=fragment;
      BottomBar bb=BottomBar.attach(this,savedInstanceState);
      bb.setActiveTabColor("RED");
  
      bb.setItemsFromMenu(R.menu.bottom_buttons, new OnMenuTabSelectedListener() {
          @Override
          public void onMenuItemSelected(int menuItemId) {
              fragmentTransaction=fragmentManager.beginTransaction();
  
              switch(menuItemId)
              {
                  case R.id.button1:
                      previous=fragment;
                     // ab.setAlpha(0);
                      fragment=recepie_fragment;//new Recepie_Fragment();
                     // fragment.setRetainInstance(true);
                      break;
                  case R.id.button2:
                      //getSupportActionBar().show();
                      previous=fragment;
                      fragment=products_fragment;//new Products_Fragment();
                      //fragment.setRetainInstance(true);
  
                      break;
                  case R.id.button3:
                      previous=fragment;
                      //getSupportActionBar().show();
                      fragment=swadeshi_fragment;//new Swadeshi_Fragment();
                      //fragment.setRetainInstance(true);
  
                      break;
              }
              fragmentTransaction.replace(R.id.bglayout,fragment);
  
              fragmentTransaction.commit();
          }
      });
  }
  </code></pre>
  
  <p>Here is my RecepieFragment.java</p>
  
  <pre><code>public class Recepie_Fragment extends Fragment {
      View rootView;
      private Toolbar toolbar;
      private TabLayout tabLayout;
      private ViewPager viewPager;
  
      int logo_ids[]={R.drawable.main_course,R.drawable.cakes,R.drawable.specials};
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
      }
      void setupTabIcons()
      {
  
          tabLayout.getTabAt(0).setIcon(logo_ids[0]);
          tabLayout.getTabAt(1).setIcon(logo_ids[1]);
          tabLayout.getTabAt(2).setIcon(R.drawable.extra);
         // tabLayout.getTabAt(1).setCustomView();
  
      }
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          //getActivity().getApplication().setTheme(R.style.AppTheme_NoActionBar);
          rootView=inflater.inflate(R.layout.recepie_viewpager, container, false);
          //getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
          toolbar=(Toolbar)rootView.findViewById(R.id.toolbar_yash);
         // ((AppCompatActivity) getActivity()).findViewById(R.id.toolbar_yash).animate().translationY(-(R.id.toolbar_yash).getBottom()).setInterpolator(new AccelerateInterpolator()).start();
  
          viewPager = (ViewPager)rootView.findViewById(R.id.viewpager);
          setupViewPager(viewPager);
          setRetainInstance(true);
          viewPager.setOffscreenPageLimit(3);
          tabLayout = (TabLayout)rootView. findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(viewPager);
          setupTabIcons();
          viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
  
              @Override
              public void onPageSelected(int position) {
                  // on changing the page
                  // make respected tab selected
                  //actionBar.setSelectedNavigationItem(position);
              }
  
              @Override
              public void onPageScrolled(int arg0, float arg1, int arg2) {
              }
  
              private int previousState, currentState;
  
              @Override
              public void onPageScrollStateChanged(int state) {
  
                  int currentPage = viewPager.getCurrentItem();       //ViewPager Type
                  if (currentPage == 2 || currentPage == 0) {
                      previousState = currentState;
                      currentState = state;
                      if (previousState == 1 &amp;&amp; currentState == 0) {
  
                          viewPager.setCurrentItem(currentPage == 0 ? 2 : 0);
                      }
                  }
              }
          });
  
          return rootView;
      }
  
      private void setupViewPager(ViewPager viewPager) {
          ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());//(getActivity().getSupportFragmentManager());
          adapter.addFrag(new Main_Course(), "");
          adapter.addFrag(new Cakes(), "");
          adapter.addFrag(new Specials(), "");
          viewPager.setAdapter(adapter);
      }
  
      class ViewPagerAdapter extends FragmentPagerAdapter {
          private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
          private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
          public ViewPagerAdapter(FragmentManager manager) {
              super(manager);
          }
  
          @Override
          public Fragment getItem(int position) {
              return mFragmentList.get(position);
          }
  
          @Override
          public int getCount() {
              return mFragmentList.size();
          }
  
          public void addFrag(Fragment fragment, String title) {
              mFragmentList.add(fragment);
              mFragmentTitleList.add(title);
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              return mFragmentTitleList.get(position);
          }
      }
  }
  </code></pre>
  
  <p>And this the xml file i am using :
         
         
  
  <pre><code>tools:context=".activity.activity.MainActivity"&gt;
  
  &lt;android.support.design.widget.AppBarLayout
  
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/appbarmain"
      android:theme="@style/AppTheme.NoActionBar"&gt;
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/toolbar_yash"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="?attr/colorPrimary"
          app:popupTheme="@style/AppTheme.NoActionBar" /&gt;
  
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;include layout="@layout/content_main" /&gt;
  
     &lt;android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|end"
      android:layout_margin="@dimen/fab_margin"
      android:src="@android:drawable/ic_dialog_email" /&gt;
  
     &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>
  
  <p>Below type of visualisation is created when I'm trying to hide the toolbar/actionbar in recepies fragment. What I want is whenever I move into products or swadeshi fragment I get this action bar but when in recepie fragment my actionbar hides and screen consists of my viewpager of 3 elements as shown of cake but instead a blank is created.</p>
  
  <p><a href="https://i.stack.imgur.com/TwRAz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TwRAz.png" alt="enter image description here"></a></p>