Search code examples
javaandroidtabsandroid-tablayout

Tablayout animation like whatsapp


I am using TabLayout and SearcView. How can I do , when click search button to hide tab layout like whatsapp. I try setEnabled method but it can't work properly. I also used Visibility,setActivated method.What can I do?I don't know. I must use setAnimation method or something else?

MainActivity.java

private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    

    private CoordinatorLayout coordinatorLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        coordinatorLayout = (CoordinatorLayout)findViewById(R.id.root_view);
        

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

       
        viewPager = (ViewPager)findViewById(R.id.view_pager);
        setUpViewPager(viewPager);
        tabLayout = (TabLayout)findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        
    }

    private void setUpViewPager(ViewPager viewPager) {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPagerAdapter.addFragment(CurrencyFragment.getInstance(),"Tracker"); // `CurrencyFragment.getInstance()` should be in `FragmentPagerAdapter.getItem()`
        viewPagerAdapter.addFragment(ConvertFragment.getInstance(),"Converter"); // `ConvertFragment.getInstance()` should be in `FragmentPagerAdapter.getItem()`
        viewPager.setAdapter(viewPagerAdapter);
    }

activity_main.xml

<?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:id="@+id/root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            app:titleTextColor="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill">

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

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

Solution

  • If you have used menu item for search then you have to follow below code:

        @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                if (item.getItemId() == android.R.id.search) // set your own id if you have different id from search. 
                {
                    tabLayout.setVisibility(View.GONE);
                    return true;
                }  
    
                return super.onOptionsItemSelected(item);            
        }
    

    When you finished with search then set tablayout visible.

    tabLayout.setVisibility(View.VISIBLE);