Must see : How To Open Specific Tab
By using default material design build an awesome UI is more easier that before. That will make developers reduce their stresses hahaha. Take a look at the image below.
Compile the design:support in your build.gradle(app : Module) :
2. Create Directory Color
Create new folder in directory res and name it as color (this is directory not a xml file, they are different)
Inside the directory color, create a new xml file. Give it name as tab_selector.xml. This selector used for coloring the text of tab's item.
There are two colors, if the tab is selected the color will change to white, if not the color will change to grey.
For the background of the TabLayout and the color of the selector line, you can choose from Colors.xml.
4. Creating Fragments
Create 3 fragments. Because number of the tab's item is 3. Tab 1 for Home, Tab 2 for Timeline, Tab 3 for Profile.
Also create next fragment for SecondFragment.java and ThirdFragment.java.
5. Creating SlidingTabAdapter.java
This adapter used for set your tab's item. Tab Home for FirstFragment, Tab Timeline for SecondFragment and Tab Profile for ThirdFragment.
Don' forget return the size of your fragment. If you used 3 fragment, so return 3 on inside method getCount().
6. Modify MainActivity.java
In this project i use MainActivity as the class where i hosting my SlidingTabAdapter. You can change with your own class.
Paste the following code :
From the code above :
Code getSupportActionBar().setElevation(0) used for hide a divider between action bar and tab layout.
private TabLayout.Tab home; private TabLayout.Tab timeline; private TabLayout.Tab profile;used for set/define your tab's item. It means you creating 3 tab's item.
home = tabLayout.newTab(); timeline = tabLayout.newTab(); profile = tabLayout.newTab();
Means home, timeline and profile as a new tabs.
tabLayout.addTab(home, 0); tabLayout.addTab(timeline,1); tabLayout.addTab(profile,2);
Means you have to define the index of your tab's item. These index will used in SlidingTabAdapter to return a fragment. For example in index 0, you will return the FirstFragment, index 1 will return SecondFragment and index 3 will return ThirdFragment.
For other explanation you can read straight from the code of MainActivity. Hope this tutorial helps you. Thank you.