How to Add Customize Tabs in Android?
In this article, custom tabs are added in android. TabLayout provides a horizontal layout to display tabs. TabLayouts can be added using viewPager also, check here, but it can not be customized. Whenever the user clicks on the tab it will lead to the transaction of one Fragment to another. Custom tabs can be created to achieve this same task. Icons, animation, text, etc according to our need can be added with tabs. Below image shows an example of a custom tab:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Step 1: Create an AlgorithmFragment by right click on java package, select new ? Fragment(Blank).
Step 2: Follow the above step for CourseFragment and LoginFragment.
Step 3: Now add the following code in the fragment_algorithm.xml file. Here a TextView is added in the layout.
Step 4: Now add the following code in fragment_course.xml file. Here a textView is added in the layout.
Step 5: Now add the following code in fragment_profile.xml file. Here a textView is added in the layout.
Step 6: Now add the following code in the tab_bar.xml file. In this file, design the layout of the custom tabs. Here for every fragment, a TextView and an icon (ImageView) is added.
Step 7: Now add the following code in the activity_main.xml file. In this file, add the layout of the custom tabs and a container for the fragment.
Step 8: Now add the following code in the MainActivity.java file. In this file, add OnNavigationItemSelectedListener that helps to navigate between the fragments. It will switch the fragment when the user taps on the icon.