2015-09-13

Android Design Support Library made our day easier by providing backward compatibility to number of material design components all the way back to Android 2.1. In Design support Library the components like navigation drawer, floating action button, snackbar, tabs, floating labels and animation frameworks were introduced. In this article we are going to learn how to implement material tabs in your apps.

Before going further, I suggest have a look at this tabs docs that defines do’s and don’ts while implementing tabs.



DOWNLOAD CODE

VIDEO DEMO

1. Making the App Material

We’ll start this by creating a new project and applying the material theme. If you are not aware of android material design, my previous article Android Getting Started with Material Design gives you a good start.

1. In Android Studio, go to File ⇒ New Project and fill all the details required to create a new project. When it prompts to select a default activity, select Blank Activity and proceed.

2. Open build.gradle and add android design support library com.android.support:design:23.0.1

3. Open colors.xml located under res ⇒ values and add the below color values.

4. Add the below dimensions to dimens.xml located under res ⇒ values.

5. Open styles.xml located under res ⇒ values and add below styles. The styles defined in this styles.xml are common to all the android versions.

6. Now under res, create a folder named values-v21. Inside values-v21, create another styles.xml with the below styles. These styles are specific to Android 5.0

7. Finally open AndroidManifest.xml and modify the theme to our customized theme by changing the android:theme attribute value.

Run the app and verify the material theme by observing the notification bar color. If you see the notification bar color changed, it means that the material design theme is applied successfully.



Now we have our app material ready. So let’s start adding the tabs. But before that we’ll create few fragment activities for testing purpose. All these fragment activities contains very simple UI with only one TextView.

8. Under your main package create a fragment named OneFragment.java and add the below code.

9. Open fragment_one.xml located under res ⇒ layout and do the below changes.

10. Likewise create few more fragment activities with same code we used for OneFragment.java. I have created TwoFragment.java, ThreeFragment.java, FourFragemnt.java ….. upto TenFragment.java

2. Fixed Tabs

Fixed tabs should be used when you have limited number of tabs. These tabs are fixed in position. In android design support library lot of new elements like CoordinatorLayout, AppBarLayout, TabLayout and lot more were introduced. I won’t cover all of these as it’s not the agenda of this article.

11. Open the layout file of main activity (activity_main.xml) and add below layout code.

app:tabMode – Defines the mode of the tab layout. In our case the value should be “fixed”

12. Open MainActivity.java and do the below changes.

tabLayout.setupWithViewPager() – Assigns the ViewPager to TabLayout.

setupViewPager() – Defines the number of tabs by setting appropriate fragment and tab name.

ViewPagerAdapter – Custom adapter class provides fragments required for the view pager.

Now run the app. You should able to see the tabs displayed with swipe functionality between the tabs.



2.1 Full Width Tabs

If you want the tabs to be occupied the fullwidth of the screen, you need to assign app:tabGravity=”fill” to our TabLayout.

2.2 Center Aligned Tabs

If you want to keep your tabs horizontally centered, assign app:tabGravity=”center” to TabLayout.

3. Scrollable Tabs

The scrollable tabs should be used when you have many number of tabs where there is insufficient space on the screen to fit all of them. To make the tabs scrollable, set app:tabMode=”scrollable” to TabLayout.

13. Open activity_main.xml and change the app:tabMode to scrollable.

14. Edit MainActivity.java and add few fragments to ViewPager in setupViewPager() method. I have added total of 10 fragments to ViewPager. After the changes, your main activity should look like below.

Now if you run the app, you can see the more number of tabs with scrollable functionality.

4. Tabs with Icon & Text

Sometimes you might wanted to add an icon to Tab. Earlier adding an icon to tab is tedious process. But with the design support library it is very easy. All you have to do is call setIcon() method by passing appropriate icon. The icon will be placed in front of tab label.

15. Open your MainActivity.java and modify the code as below. Here I have added a new method called setupTabIcons() in which I have set all the tab icons.

5. Tabs with only Icons

Setting only icon to tab is same as setting text and icon except the method getPageTitle() in ViewPagerAdapter class returns null instead of tab label.

16. Open MainActivity.java and modify the getPageTitle() method as below and run the project.

6. Custom Tab View with Icon & Text

Setting a custom view to the tab is very useful when you are not able to achieve desired output by following the methods provided by tab layout. While setting a custom view to tab, make sure that you follow the specs suggested by android for tabs.

When we set the tab an icon and text, you can see the icon is horizontally aligned with tab text. But if you want to place the icon above the tab label, you have to use a custom view to achive it.

17. Under res ⇒ values, create an xml file named fonts.xml and add below string value. This xml file defines the font family for the tab label.

18. Under res ⇒ values-v21, create another xml named fonts.xml.

19. Open activity_main.xml and set the custom height to TabLayout. Setting this height is important as placing icon above the tab label takes more space than normal.

20. Create an xml layout named custom_tab.xml under res ⇒ layout. In this layout we have defined the custom view for the tab.

21. Open MainActivity.java and modify the code as below. Here if you observe setupTabIcons() method, I have rendered custom_tab.xml layout in each tab using below lines of code.

Now if you run the app, you can see the icon placed above the tab label.

I hope this article provided useful information about the tab layout using design support library. If you have any queries please do comment below.

Show more