Tablayout Android Kotlin With Different Actionbar/Toolbar MenuItems For Different Fragments

This is a tablayout android kotlin example.

Tablayout android kotlin tutorial will guide you to make different actionbar/toolbar menuitems for different colors.

In whatsapp, when you slide fragments menu items are also changing.

We will develop this functionality in tablayout android tutorial.

If you don’t have go through basic tablayout tutorial, then visit android tablayout kotlin tutorial.

Develop basic tablayout example from above link then follow steps of this tutorial.

First, check the output of this example.

Download Source Code for Tablayout Android Kotlin example

[sociallocker]Download KotlinTabMenuItem[/sociallocker]

Step 1: Make Basic structure

Follow all the steps from basic tablayout Android tutorial.

After you have completed above tutorial, you will have one android project with tablayout.

Now follow below steps.

Step 2: Adding images to drawable

Download images and copy them into “drawable” directory

TabMenu Icons

Step 3: Updating styles.xml

Update styles.xml file as per below source code

Step 4: Creating toolbar.xml file

Create a new resource file named toolbar.xml and paste below

Step 5: Developing OneActivity

Create a new activity named “OneActivity.kt

Copy following in activity_one.xml

Add following source code into OneActivity.kt class

Step 6: Creating menu resource directory

Create a new resource directory named “menu” inside the “res” directory.

Inside “menu,” make three new xml files and name them as one_menu.xml,two_menu.xml and three_menu.xml

Add below source code in one_menu.xml

Add following in two_menu.xml

Add below in three_menu.xml

Step 7: Updating all fragments

Update OneFragment.kt as per below code

Update TwoFragment.kt as per following

Update ThreeFragment.kt like below

Java Version

Java version of this tutorial: Tablayout Android

Above tutorial will helpful if you are not familiar with kotlin.

Use comment section for queries and reviews. Thank you.

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

1 Response

  1. Benaida Amel says:

    Thank you thank you thank you
    the only tutorial that worked for me

Leave a Reply

Your email address will not be published. Required fields are marked *