Android Tablayout Kotlin With Fragments,ViewPager,Material Design

Whatsapp is using Android Tablayout Kotlin for basic navigation.

In today’s Android Tablayout Kotlin example, we will develop that kind of structure programmatically.

Android Tablayout Kotlin uses fragments, material design and view pager as basic components.

You will be able to make a project where user can swipe left or right to switch between tabs.

This method is used by whatsapp also.

First, watch the output, then we will prepare it.

Download Source Code for android tablayout kotlin example

[sociallocker]Download Kotlin_SimpleTabs[/sociallocker]

Step 1: Create a new project in Android Studio.

Consider empty activity as a main activity when you create a fresh new project in android studio.

Step 2: Update build.gradle(Module:app) file

Add below in build.gradle(Module:app) file

Above two lines will give access to necessary classes to use material design in our app.

Whole source code for build.gradle(Module:app)

Step 3: Updating colors.xml

Go to the res->values->colors.xml directory

update colors.xml as below

Step 4: Preparing three fragments

Now we need to make fragment per each tabs.

Means that if you want three tabs than make three fragments, if you want five tabs than make five fragments.

We will add three tabs in this tutorial.

You can update the look and feel of every tab by adding required code to their fragment’s xml or .kt files.

Create three fragments and give them name as “OneFragment,” “TwoFragment,” and “ThreeFragment.

Add below code in fragment_one.xml

Add following in OneFragment.kt

Copy below source code in fragment_two.xml

Copy following code in TwoFragment.kt

Add below code in fragment_three.xml

Paste below code in ThreeFragment.kt

Step 5: Updating MainActivity

update activity_main.xml as below

Update MainActivity.kt as per following

Java Version

Java Version of this tutorial : Android TabLayout

That is all for Android tablayout Kotlin example.

If you like this tutorial then please share it in your social media.

Comment if you have queries or a review. Thank you for reading.

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...

2 Responses

  1. Tushar says:

    Hi, I can not view your java code for this fragment example, could you please share.

Leave a Reply

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