Customizing TabLayout Buttons

Viewed 4037

Currently I have the following looks of TabLayout:

enter image description here

and it's code is a normal TabLayout:

<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"/>

and I want to achieve the following style:

enter image description here

How to possibly achieve this? the function of the button at the center is to call an activity and the TabLayout will be place at the bottom of the screen.

NOTE: I actually have 4 Tabs and not just 2

this was my code before

<android.support.constraint.ConstraintLayout 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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.sampleproject.MainActivity"
        tools:showIn="@layout/activity_main">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:orientation="vertical">

               <!--SOME CODE HERE-->

            <android.support.v7.widget.CardView
                android:id="@+id/bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/top"
                android:orientation="horizontal"
                app:cardBackgroundColor="@color/colorWhite"
                app:cardCornerRadius="0dp"
                app:cardElevation="20dp"
                app:cardUseCompatPadding="false"
                tools:layout_editor_absoluteY="0dp"
                tools:layout_editor_absoluteX="8dp">

                <LinearLayout
                    android:id="@+id/bottom_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorWhite"
                    android:gravity="center_horizontal"
                    android:minHeight="48dp"
                    android:orientation="horizontal">

                    <ImageButton
                        style="@style/Base.Widget.AppCompat.ActionButton"
                        android:layout_width="0dp"
                        android:layout_height="?attr/actionBarSize"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:src="@drawable/ic_toolbar_messages" />

                    <!--START OF FAKE BUTTON, this button is here to create space between the two ImageButton -->
                    <ImageButton
                        style="@style/Base.Widget.AppCompat.ActionButton"
                        android:layout_width="0dp"
                        android:layout_height="?attr/actionBarSize"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:src="@null" />
                    <!--END OF FAKE BUTTON-->

                    <ImageButton
                        style="@style/Base.Widget.AppCompat.ActionButton"
                        android:layout_width="0dp"
                        android:layout_height="?attr/actionBarSize"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:src="@drawable/ic_drawer_contact" />   


                </LinearLayout>
            </android.support.v7.widget.CardView>

        </LinearLayout>
    </android.support.constraint.ConstraintLayout>

and I'm using a FloatingActionButton and place it on the bottom|center

<android.support.design.widget.FloatingActionButton
        android:id="@+id/fabNewConversation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_add" />

I just decided to used TabLayout but I have no idea on how to apply my style.

Thanks!

UPDATE

seems like BottomNavigationView is what I want to be happen, but it failed to meet my requirement. I also don't want to use external library for this. So what I did is just remove the TabLayout and put the ViewPager at the top of the parent and my custom TabLayout style in the bottom. One of my requirement is to disable the swipe of ViewPager so I disabled the swipe on it by using custom class and trigger the pages onClick of ImageButton. Thanks for answers and suggestions. I'll put my code later.

UPDATE: HERE IS MY CODE

First I created a class NonSwipeableViewPager for Custom ViewPager, this will disable the swipe function of it.(I searched for this, I'm not actually the author of this NonSwipeableViewPager class)

public class NonSwipeableViewPager extends ViewPager {

    public NonSwipeableViewPager(Context context) {
        super(context);
        setMyScroller();
    }

    public NonSwipeableViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        setMyScroller();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }

    //down one is added for smooth scrolling

    private void setMyScroller() {
        try {
            Class<?> viewpager = ViewPager.class;
            Field scroller = viewpager.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            scroller.set(this, new MyScroller(getContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class MyScroller extends Scroller {
        public MyScroller(Context context) {
            super(context, new DecelerateInterpolator());
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
        }
    }

    //Disable Left and Right arrow swipe for keyboard in some devices
    @Override public boolean executeKeyEvent(KeyEvent event) { return false; }

}

second, Initialize content_main.xml layout. Remember! This is the content_main.xml, I @include this from activity_main.xml where the ToolBar and FloatingActionButton view are initialize.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.sample.MainActivity"
    tools:showIn="@layout/activity_main">

    <!--I use LinearLayout to be the container_parent-->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:orientation="vertical">

        <!--TOP VIEW-->
        <RelativeLayout
            android:id="@+id/top"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <!--Initialize the Custom ViewPager here, Make sure you created the class for this-->
            <com.sample.NonSwipeableViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <!--END OF CUSTOM VIEWPAGER -->

        </RelativeLayout>
        <!--END OF TOP VIEW-->

        <!--BOTTOM VIEW-->
        <android.support.v7.widget.CardView
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/top"
            android:orientation="horizontal"
            app:cardBackgroundColor="@color/colorWhite"
            app:cardCornerRadius="0dp"
            app:cardElevation="20dp"
            app:cardUseCompatPadding="false"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="8dp">

            <LinearLayout
                android:id="@+id/bottom_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorWhite"
                android:gravity="center_horizontal"
                android:minHeight="48dp"
                android:orientation="horizontal">

                <ImageButton
                    android:id="@+id/btn_one"
                    style="@style/Base.Widget.AppCompat.ActionButton"
                    android:layout_width="0dp"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@drawable/ic_one" />

                <ImageButton
                    android:id="@+id/btn_two"
                    style="@style/Base.Widget.AppCompat.ActionButton"
                    android:layout_width="0dp"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@drawable/ic_two" />

                <!--START OF FAKE BUTTON FOR SPACER-->
                <ImageButton
                    android:id="@+id/fake_btn_spacer"
                    style="@style/Base.Widget.AppCompat.ActionButton"
                    android:layout_width="0dp"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@null"
                    android:clickable="false" />
                <!--END OF START OF FAKE BUTTON FOR SPACER-->

                <ImageButton
                    android:id="@+id/btn_three"
                    style="@style/Base.Widget.AppCompat.ActionButton"
                    android:layout_width="0dp"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@drawable/ic_three" />

                <ImageButton
                    android:id="@+id/btn_four"
                    style="@style/Base.Widget.AppCompat.ActionButton"
                    android:layout_width="0dp"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:src="@drawable/ic_four" />

            </LinearLayout>

        </android.support.v7.widget.CardView>
        <!--END OF BOTTOM VIEW-->

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

third, the activity_main.xml layout

<?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"
    tools:context="com.sample.MainActivity">

    <!--SOME CODE FOR MY AppBarLayout-->
    <!--SOME CODE FOR MY ToolBar-->

    <!--INCLUDE content_main.xml here-->
    <include layout="@layout/content_main" />

    <!--START OF FloatingActionButton and set it to bottom|center-->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_add" />

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

lastly, the MainActivity.class

public class MainActivity extends AppCompatActivity {

    private NonSwipeableViewPager viewPager; //Declare Custom ViewPager Class
    ImageButton btn_one, btn_two, btn_three, btn_four; //Declare ImageButton

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

        /**SOME CODE HERE**/

       //Initialize ImageButton
       btn_messages = (ImageButton) findViewById(R.id.btn_one);
       btn_people = (ImageButton) findViewById(R.id.btn_two);
       btn_video_call = (ImageButton) findViewById(R.id.btn_three);
       btn_voice_call = (ImageButton) findViewById(R.id.btn_four);

       //Initialize FloatingActionButton which in the Center at the Bottom of the Parent
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Handle some functions here...
            }
        });

        //Initialize ViewPager
        viewPager = (NonSwipeableViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager); //Call setupViewPager to setup the ViewPager

        //Initialize OnClickListener in every ImageButton
        btn_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0); //Trigger Page 1 of ViewPager
            }
        });

        btn_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1); //Trigger Page 2 of ViewPager
            }
        });

        btn_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2); //Trigger Page 3 of ViewPager
            }
        });

        btn_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(3); //Trigger Page 4 of ViewPager
            }
        });

    }

    //This function setup the ViewPager
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new Fragment_Zero(), "ZERO");
        adapter.addFragment(new Fragment_One(), "ONE");
        adapter.addFragment(new Fragment_Two(), "TWO");
        adapter.addFragment(new Fragment_Three(), "THREE");
        viewPager.setAdapter(adapter);
    }

    //Inner Class for ViewPagerAdapter
    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        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 addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
            // return mFragmentTitleList.get(position);
        }
    }
    //END
}

Output:

enter image description here

3 Answers

Use ConstraintLayout and add Left,Right and Bottom constraint to it.

<android.support.constraint.ConstraintLayout
---
---
>

<android.support.design.widget.FloatingActionButton
...
...
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent”"
app:layout_constraintRight_toRightOf="parent"  />
</Constraint Layout>

Place your other views inside it.

  • Gradle Dependency :

    compile 'com.android.support.constraint:constraint-layout:1.0.2'

To learn more about ConstraintLayout

Related