Search code examples
androidandroid-tablayoutfloating-action-button

FloatingActionButton Icon disappear after using hide() function


So i have tab layout with 4 fragment inside it, i want my FAB change icon according to selected tab. But the problem is after using fab.hide()function the icon is disappear. this is what i get

here is my code:

private void fabSettings() {
    mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            int position = tab.getPosition();
            switch (position) {
                case 0:
                    mFab.hide();
                    break;
                case 1:
                    mFab.show();
                    mFab.setImageResource(R.drawable.ic_comment_white_24dp);
                    break;
                case 2:
                    mFab.show();
                    mFab.setImageResource(R.drawable.ic_camera_alt_white_24dp);
                    break;
                default:
                    mFab.show();
                    mFab.setImageResource(R.drawable.ic_phone_black_white_24dp);

            }

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

I want when tab at index 0 selected the FAB is hidden.


Solution

  • Add three FloatingActionButton in your main activity like below:

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">
    
            </android.support.v7.widget.Toolbar>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabM"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_fab_M" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabChat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_fab_chat" />
    
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabPerson"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_fab_person"
            android:visibility="gone" />
    

    Now in your MainActivity.java use Fab's default functions to hide and show on each tab selection like below :

    private void animateFab(int position) {
        switch (position) {
            case 0:
                fabChat.show();
                fabPerson.hide();
                break;
            case 1:
                fabPerson.show();
                fabChat.hide();
                break;
            case 2:
               fabM.show();
               fabPerson.hide();
    
            default:
                fabChat.show();
                fabPerson.hide();
                break;
        }
    }
    

    Call animateFab function as below:

    TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            animateFab(tab.getPosition());
        }
    
        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
    
        }
    
        @Override
        public void onTabReselected(TabLayout.Tab tab) {
    
        }
    };
    
    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
        }
    
        @Override
        public void onPageSelected(int position) {
            animateFab(position);
        }
    
        @Override
        public void onPageScrollStateChanged(int state) {
    
        }
    };