Search code examples
androidandroid-fragmentsmobileandroid-viewpagerandroid-tablayout

Hide Tablayout Bar in android


I have a activity with toolbar ,Tablayout , viewpager with fragments like that

enter image description here I want to implement toolbar material search on all the fragments

like that enter image description here

but the problem is TAB Bar . I want to hide the tab bar(Call,Chat,Contact) on Search Open and unhide it when the search close

How can I hide the tabbar ?

I am using this library for material Search view implementation

layout code is below

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
   >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
        <FrameLayout
            android:id="@+id/toolbar_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"

            android:minHeight="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <com.miguelcatalan.materialsearchview.MaterialSearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        </FrameLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="scrollable"
            app:tabGravity="center"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_dashboard" />


</android.support.design.widget.CoordinatorLayout>

Solution

  • here is your solution, I tried this in fragment.

    1. Write two methods like below in your Main Activity which having tablayout for hiding and showing tablayout.

        public static void showTabLayout() {
                    parentTabs.setVisibility(View.VISIBLE);
                }
    
        public static void hideTabLayout() {
                    parentTabs.setVisibility(View.GONE);
    
                }
    

    2.Create Interface

     public interface FragmentLifecycle {
    
            public void onPauseFragment();
            public void onResumeFragment();
    
        }
    

    3. Implement interface and call hide/show tab-layout methods

    CameraFragment.java

    package demo.com.demo;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    /**
     * Created by rucha on 24/11/16.
     */
    public class CameraFragment extends Fragment implements FragmentLifecycle {
    
        public CameraFragment() {
            // Required empty public constructor
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = null;
            rootView = inflater.inflate(R.layout.fragment_blue,
                    container, false);
            setupUI(rootView);
            return rootView;
        }
    
        private void setupUI(View rootView) {
        }
    
    
        @Override
        public void onPauseFragment() {
            MainActivity.showTabLayout();
        }
    
        @Override
        public void onResumeFragment() {
            MainActivity.hideTabLayout();
        }
    }
    

    I hope this is useful. Happy Coding!!