One of the best things about technology is that it keeps on changing. On the same lines recently some significant improvements were made for Android by the means of support library. The new Design Support Library was included in the support repository. This new Android Design Support library features many new UI components like snackbars, floating action buttons and of-course it significantly improves the implementation of Android Tabs. Since Android design support library contains many new UI widgets which were introduced as concepts with material design. I would write separate tutorials for each one of those. Here lets concentrate on the Android tabs example.
Earlier to make tabs in Android, action bar was used. But now with API 21 onwards it has been deprecated. An alternative way to make tabs with full backward support was to use SlidingTabLayout
and SlidingTabStrip
classes. But now with Android Design Support Library making swipe tabs has become even more simpler. In this Android Tabs example lets explore the power of new design support library.
Making an Android Tabs Example by using Android Design Support Library
Tab type navigation mode is a very common design pattern among android apps. But since Android’s 5.0 release, material design came in to picture and allot of changes were made in various APIs. Resulting the deprecation of action bar. Although a new API, Android Toolbar was released to replace it. Due to this change, new APIs for android tabs were also released through the design support library recently. The main class used for displaying tabs through these new APIs is Android TabLayout. In this Android Tab example we would make a screen with three tabs using these new APIs for tabs with Fragment
s and a ViewPager
which would look like the image below:
To start off please include these libraries in the dependencies section of your build.gradle file:
compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0'
Now since we will be using Android Toolbar and TabLayout classes to show tabs, lets remove the action bar from layout by using styles:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- your app branding color for the app bar --> <item name="colorPrimary">#3F51B5</item> <!-- darker variant for the status bar and contextual app bars --> <item name="colorPrimaryDark">#303F9F</item> <!-- theme UI controls like checkboxes and text fields --> <item name="colorAccent">#FF4081</item> </style> </resources>
To get a better understanding on Android Toolbar and its usage have a look at this Android Toolbar tutorial.
Next, to display Android tabs with fragment and ViewPager, lets define three simple fragments and their layouts:
package com.truiton.designsupporttabs; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabFragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_fragment_1, container, false); } }
Its layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Tab 1" android:textAppearance="?android:attr/textAppearanceLarge"/> </RelativeLayout>
Android Tab 2:
package com.truiton.designsupporttabs; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabFragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_fragment_2, container, false); } }
Its layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Tab 2" android:textAppearance="?android:attr/textAppearanceLarge"/> </RelativeLayout>
Android Tab 3:
package com.truiton.designsupporttabs; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class TabFragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.tab_fragment_3, container, false); } }
Its layout:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Tab 3" android:textAppearance="?android:attr/textAppearanceLarge"/> </RelativeLayout>
Now that we have all the tab fragments defined, lets define a view pager adapter for the swipe tabs feature:
package com.truiton.designsupporttabs; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; public class PagerAdapter extends FragmentStatePagerAdapter { int mNumOfTabs; public PagerAdapter(FragmentManager fm, int NumOfTabs) { super(fm); this.mNumOfTabs = NumOfTabs; } @Override public Fragment getItem(int position) { switch (position) { case 0: TabFragment1 tab1 = new TabFragment1(); return tab1; case 1: TabFragment2 tab2 = new TabFragment2(); return tab2; case 2: TabFragment3 tab3 = new TabFragment3(); return tab3; default: return null; } } @Override public int getCount() { return mNumOfTabs; } }
In the view state pager adapter above, you may see that I have just initialized the fragments as per their location. Next lets define the layout for main activity where all these tabs would be displayed.
Displaying Tabs using Android TabLayout
<RelativeLayout android:id="@+id/main_layout" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:background="?attr/colorPrimary" android:elevation="6dp" android:minHeight="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/toolbar" android:background="?attr/colorPrimary" android:elevation="6dp" android:minHeight="?attr/actionBarSize" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_below="@id/tab_layout"/> </RelativeLayout>
Earlier tabs were added to the action bar through code. But as you can see in this Android tabs example layout above we are using Toolbar and the TabLayout separately to display tabs. Also please note a view pager is also added, which would be attached to this TabLayout in the activity below through code.
package com.truiton.designsupporttabs; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout); tabLayout.addTab(tabLayout.newTab().setText("Tab 1")); tabLayout.addTab(tabLayout.newTab().setText("Tab 2")); tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); final ViewPager viewPager = (ViewPager) findViewById(R.id.pager); final PagerAdapter adapter = new PagerAdapter (getSupportFragmentManager(), tabLayout.getTabCount()); viewPager.setAdapter(adapter); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
The above class concludes this Android tab example with Fragments and ViewPager. As you can see that, above we have used the Android Toolbar instead of action bar and tabs are further added in the TabLayout through code with text. Then you can see that ViewPager is attached with an adapter using the ViewPager.setAdapter(adapter)
method. Next Android ViewPager is attached to a page change listener of TabLayout by using the method ViewPager.addOnPageChangeListener
. Further the Android TabLayout is attached to a tab selected listener using the TabLayout.setOnTabSelectedListener
method, in which ViewPager’s page is set when a tab is selected. This whole piece of code would make an Android Tab example which would run on Android 7+ APIs. Hope this helps. Connect with us on Facebook, Google+ and Twitter for more updates.
Born in New Delhi, India. A software engineer by profession, an android enthusiast and an evangelist. My motive here is to create a group of skilled developers, who can develop something new and good. Reason being programming is my passion, and also it feels good to make a device do something you want. In a very short span of time professionally I have worked with many tech firms. As of now too, I am employed as a senior engineer in a leading tech company. In total I may have worked on more than 20 projects professionally, and whenever I get spare time I share my thoughts here at Truiton.
Hi. Brilliant and clean/simple tutorial 🙂 How can I use strings for the names of my tabs rather than hard-coding them?
Thanks.
Well to change the names you can use
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
in a loop and use a string as per your convenience.Excellent. Many thanks. I’ll use .setText.(R.string.tab_name) 😀
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:design:22.2.0'
Getting This below Error how to solve it
Failed to find compile ‘com.android.support:design:22.2.0’
I believe you need to download and update your SDK using the SDK manager, especially the support library and support repository.
Check Your TargetSDK Version in module(app.gradle)
…according to that put 22,23,24
example your target sdk is 24 than put
compile ‘com.android.support:appcompat-v7:24.0.0’
compile ‘com.android.support:design:24.0.0’
You need to change
compile ‘com.android.support:appcompat-v7:22.0.0’
compile ‘com.android.support:design:22.2.0’
to
compile ‘com.android.support:appcompat-v7:28.0.0’
implementation ‘com.android.support:design:28.0.0’
this showed no error to me
Hi. Any direction on how to implement this tab layout in one of the navigation view item clicked. Do I just put the code from MainActivity in to the item’s fragment class?
Hey, Thanks for the tutorial.
I have one query. How can I change the Font size of Tabs, because my Tab’s text are quite big so they not set in single line.(Or is it possible to swap Tabs if in case I don’t want to font size).
yes you can change the font size of tabs. You need to override its styles in the Styles.xml.
Hey, Thanks for example. I’m having one issue, when I swipe on ViewPager is working perfect but when I swipe from from left to right or right to left and select previous Tab: Tab indicator appear on new selected Tab but respective fragment not appear on ViewPager.
Please help me, where I’m getting wrong?
Hi, How to add webview in different tabs!!
Thx my friend!! excelent tuto
Very nice! Thank you.
If I want to insert a new ListView in Tab #1, where do I place the JAVA code of the ListView?
It doese’t make sense at TabFragment1.java
Thank you for the tutorial. It works great. But I’m having a problem when changing orientation. I removed
android:orientation="vertical"
but still stopping the application. What could be wrong?I found the fix, simply add
android:configChanges="orientation|screenSize"
to the activity on manifiestThis is not a solution, it is a patch that will present issues further down the track 99.9% of the time.
I want load only the selected tab to be loaded i don’t want to load the adjacent tab into the memory how can i do this. If click on the selected again it should get refresh every time i select to the tab
How to change text color of tabs ??
Great tutorial. Worked flawlesly.
Hi! Great tutorial, I’m a beginner in android and thanks to you I’ve understand many things.
I need to add a listview in one of the tabfragments, but I can’t find the right way, can you help me?
Thanks a lot!
Hi Stefano,
Thanks, for your problem you can take inputs form this tutorial:
https://www.truiton.com/2013/05/android-fragmentstatepageradapter-example/
-Mohit
i need to show icons and text both on tabs and to change color when it is selected/unselected..please help
Same Questions.. please help me..
hey thanks a lot ,
was really helpfull~~
u make a nice tutor!!
you should start a video tutorial series~~~~~
bst of luck myaaaan…..
I need some more width for my text and Icon. how do i make my tabs with large.
Hello,
First of all, Thank you for this great tutorial.
i have a small question :
Let’s say i have a new Fragment “SecondFragmentInTab1” , and i want to open it in the Tab 1 after clicking some button in the fragment TabFragment1 .
How can i do this ?
Thank you,
Houcine
same question hele me too???
Did you find a solution to this ???
Check on the MAster/Detail Pattern.
Nice tutorial !!
How can I switch tabs programmatically? eg : If Tab 1 has a list of items on click of which I want to display the details of that item in Tab 2..
Thank you Mohit 🙂
Great tutorial, everythings works fine, even if i’m using images instead of text.
But, right now, I want that when a tab is selected, the image of the selected tab change (to show the user where he is changing color of the selected tab).
I have really no idea of how I can do this, may be a :
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
would made the thing working
Thank you for your easily, standard and neat code for Android Tabs.I hope you will make more android code for us further.Thanks once again.
Thanks man! I’m currently studying Murachs Android programming which is quite outdated. Thanks to this I’ve managed to make a task list with TabLayout including savedInstanceState.
Great article! Works like a boss on Android Studio! Thank You
Hey man Thanks a ton… Simple explanation and no errors.
I don’t know how to thank you but in one word, thank you very much for your brilliant effort done over here,
Hi, It’s great. I’m an amateur programer. Please answer.
If I put button on tab 1, where i should put on click listener? Mainactivity.java, or TabFragment1.java, or else?
You should put its OnClickListener in the TabFragment1.java.
did you add buttons in tabs?
Ebates an app i want you to see..and please tell me how that feautred design is made…while scolling up in featured option in navigation drawer images view hides and tab come below action bar of navigation drawer….it will be very thankfull if you reply 🙂
I am not sure what you are trying to ask. But if you want to hide the action bar/ Toolbar while scrolling up you can use the
CoordinatorLayout
.Hi,
Very neat tutorial.
I want to include tabLayout in one of the fragments of my slide menu.
My slide menu is working fine as seperate. But how can I combine both of these for one of the fragments in my menu.
Thanks in advance for help.
Great tutorial, easy to read and understand !
Be now we can simplify it a little more 🙂
Instead of using “viewPager.addOnPageChangeListener” and “tabLayout.setOnTabSelectedListener” we can now directly use “tabLayout.setUpWithViewPager(viewPager);” and it will works just fine !
How can we implement new scrolling tecniques to viewpager??
Hello and thanks for a most excellent tutorial!
If you could be bothered, would you please explain how would I reference the View inflated by each fragment from the MainActivity? Explaining myself, in my app, each fragment in the TabLayout represents a portion of some class’s data. I want to be able to create an instance of the class in my MainActivity and pass it references to each fragment’s inflated view so that the instance is able to reference each view’s layout components in order to update its value.
Do I make sense? 🙂
Thanks again!
hi mohit setOnPageChangeListener() is deprecated . what method should be use to implement that ?
Thanks 🙂
Hi,
You can use
addOnPageChangeListener
instead of it.Hope it helps.
How to Implemetnt Listview in tablayout?
Add the listview/recyclerview in the fragment.
thank you very much Mohit
Hello mohit can u please help me for how to change background color of selected tab…
Thanks dear friend it works
Very good/clean tutorial. Thank you.
Thank you :)))
Hiii
Very nice tutorial thanks for knowledge sharing.
I have a little problem, i.e I want to add 6 tabs and it is working but all tabs are displayed on same screen size i want to have a scrolling effect to tabs section, how can i do it as after giving names to the tabs every tabs shrinks and its bit difficult to read…
Can you please help me on this.
Thanks
Hi Vipul,
Please use
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
to make your tabs scroll-able.-Thanks
Saved my days when my search ended here
Thanks dude
Hi Mohit!
Thanks very much for this, it’s brilliant!
One question, how to enable “white” divider between each tab?
I have tried this in the tab_layout, but still it does not show a divider, can you help please?
android:divider=”@android:color/white”
android:showDividers=”middle”
android:dividerPadding=”1dp”
Thanks!
Matt
hello sir i want to show 10+ tab in the same can you please tell me how to do that
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
Hello sir, I’m following your tutorial and thanks to you I have created my own tab layout but I want my page to be changed not by swiping but clicking only the names of the tabs at the top.
I’ve done both swiping and clicking the tabs but I only want to change the page when clicking the tab names. Any idea on how should I do that?
thank you sir.
thank you for the tutorial sir. Your code is excellent
How do you use each tab afterwords? use as in how do you manipulate each tab as if they were an activity? I can’t figure out how to use the simple setText(“Hello”) method after making the tabs.
maybe this tutorial would help:
https://www.truiton.com/2015/12/android-activity-fragment-communication/
This is by far the best example I have found for using tabs and fragments – and I have read a LOT of tutorials!
Thank you so much!
Nice work……Thanks Bro.
Hi Mohit .Thanks for the great tutorial.I have one small query.I have one activity and 5 fragments in the sliding tab layout.Can you please tell me what would be the best way to communicate between them.I need to send bundles across .
Please refer to this tutorial:
https://www.truiton.com/2015/12/android-activity-fragment-communication/
excelent
Thank You Mohit For such amazing tutorial. I have 1 doubt ,If possible then please help me. After user logged in They are redirected to tabs activity(according to above tuts). I want to print users detail in first fragment .But became bit confused how to actually implement it inside fragment Because we have 1 activity inside which there are 3 fragments . Please simply guide me. Thank you in advanced.
Hi. Thanks for sharing this tutorial. I’m getting error on this line:
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount());
The error states:
‘PagerAdapter’ is abstract; cannot be instantiated
Can you please explain how to solve this?
I believe you may have imported the wrong class i.e. android.support.v4.view.PagerAdapter.
Instead you need to import the PagerAdapter class defined in the tutorial above.
-Mohit
Yes you are right. I solved it. It was due to similarity of your class with the original one. thanks.
I have 4 Tabs, in the 4 Tabs using SlidingTabLayout, I have validation alert in 4 tab onCreateView Mehod if mVar == null {toast} ,
when i click 3 tab its throwing 4 tab Validation message,
how to Avoid this.
Please guide
FragmentStatePagerAdapter always loads the next fragment in memory, that is why your validation message is being triggered.
Therefore change your logic. Generally its a good practice to trigger validation logic after user performs some action.
Hai…I have 4 fragment in view pager using fragment page adapter…when i select 3rd position tab,calling 0th position tab…i cant understand why this happening,i have api call for every tabs…i need to call api when i select that particular tab….plz help me…
Thank you for saving me
Nice job. Thank’s
I’m new to android development and Its working! Thanks Mr. Mohit Gupt you solved my problem.
Thanks …
Used your tutorial, all works fine.
Thank you! 🙂
Hello sir. I am beginner in android programming. I found your website very helpful. You r the man of the century. Keep it up.
I am following this tutorial from beginning and everything is working fine. Could you please tell me how can i add icon+text to tabs? It will be appreciated. Thanks in advance.
To set icon on a tab use:
tabLayout.getTabAt(i).setIcon(R.drawable.iconId);
Thanks
Hiii sir, i want to add total 6 tabs but when i created 6 tabs.tabs created very close.please help me
Please use
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
to make your tabs scroll-able.How to set value in fragemtn fields dynamically
OMG! This was so easily described. Thanks a huge bunch! May Allah bring you to success!
Can you please share working project ( Eclipse Version )
Thank you Mohit for the fantastic tutorial! Worked like a charm.
Reader: You should try this method if you’re having trouble with deprecated classes and functions, and just need a simple Tab control implemented that (seems to be) supported on old API versions. I am using the following libraries in build.gradle with a Minimum compatibility of API 9, and so far so good:
dependencies {
…
compile ‘com.android.support:appcompat-v7:23.2.1’
compile ‘com.android.support:design:23.2.1’
compile ‘com.android.support:support-v4:23.2.1’
…
}
Hello, using your example, it’s possible to pass arguments (parameters) to fragments tabs ?
To do so, please refer to this tutorial:
https://www.truiton.com/2015/12/android-activity-fragment-communication/
I am getting “Cannot resolve symbol” error with (R.menu.menu_main, menu) and (R.id.action_settings) in the MainActivity.java file. I’m guessing for the “R.menu.menu” error is due to the fact that I do not have a res/menu.xml file, am I correct in saying this? If so, what value are needed for this file? Help with these two problems would be greatly appreciated. Love your work.
Yes you are correct in saying this. Hence you can either create a
R.menu.menu_main
file withR.id.action_settings
or you can remove these methods.TNX
Thanks man, after much searching found a clean/simple tutorial that works. Your explanation of the concepts was excellent, keep up the good work.
How can i for example use a listview filled wit data coming from a database, i can do it on a normal activity but not in the fragment, can you give some tips ? Thanks a lot.
Thanks a lot man !
great tut . i got one question , how can we set the custom font for tab bars titles?
help appreciated .
I want to use this in a Fragment not In the MainActivity , How can i do this
lovely!
how would you go about using one single view (no more viewpager swiping), and each tab that is selected, loads the data into the same view, replacing what was shown before
Great tutorial
Thanks a lot 🙂
Very nice example! How can i do a .addToBackStack(“MY_FRAGMENT”) to capture in my main activity onBackPressed event to do something with “MY_FRAGMENT”?
very,very,very helpfull website .I appreciate you for providing such a wonderfull work
Thank you very much
Very nice.
Thank you!
Hi Mohit Gupt
Thank you for tutorial
How to refresh fragment in view pager?
please help.
I am using your example. I am having a hard time trying to adjust the padding for each tab. The padding seems so big that it cuts off the tab labels too much. Setting the padding in XML of programatically seems to do nothing.
Awesome. I want to add map in tab, but unable to do so. Any suggestion?
Learnt a lot. Thank you very much. This tutorial saved my project.
Thank you very much man, i’ve been looking for an implementation about this tabs for a weak and none was so easy and clear like yours.
Hi Mohit!
The tabs are appearing in the middle of the screen!
What am I doing wrong?
Thanks a lot for the tutorial! It is really good 🙂
hi i’m new in android programing and a i don’t speak english very well so sorry if you notice wrong word in my comment
well i have a problem using your tutorial.
when i switch to the tab1 for example i got sometime the fragment 2 or fragment 3 and same for the other tabs would somebody help me please
Thanks for your tutorial. Nice and clean 🙂
Thanks a lot . Very useful for me 🙂
Do I need the Toolbar? I tried removing in activity_main.xml and it works fine. I was thinking of making an app where there is a small “hamburger” button on left instead of using a toolbar. Of course, I need a button or something (smaller toolbar perhaps) for the menu button with that approach.
Thanks for your tutorial. Easy to follow, keep doing good job 😀
by adding button on each fragment how to go to another fragment with data ??
I am creating an Android Reminder App. So, I have a Navigation Drawer. Now, Consider, I have 3 Categories . My First category consists of 3 tabs, 2nd category consists of 4 tabs, and 3rd category consists of 2 tabs . So, I have 3 Activities to host all the tab fragments in them. I don’t want any independent fragment to open. I want to click on Navigation drawer item and go to particular tab on tab layout.
Now , when I start it opens fragment one of first Activity .(very good). Then , I can go to fragment2 and fragment3 . But, If I click on Fragment 4, Fragment 5, Fragment 5 then it remains in Fragment3, coz they are in different activity. So, I need to open that activity and then I am able to go to Fragment 4, Fragment5, Fragment 6 and then again I click on Frament 1, 2 or 3 . It remains in the last opened fragment .
It was very helpful to me. I add in the first tab an alert dialog. I want to passing to second tab when user click on yes button on the alert dialog. I couldn’t make it. I couldn’t access the pager in main activity.
In my case main activity navigation into fragment ….so there no main activity …now i want do tab in fragment…any help
Hi thank you for your tutorials.
Hi thanks for great tutorial its very clear and i have question i created a button in side tabs and here is my question up on clicking that button i wanted to open a new activity i request you to help me in this please thanks in advance have a great day.
but what about menu where and how to bring the menu
Hi,
Thanks. The example works just fine!!
Is it possible to define the tablayout to show inside just one fragment and it works exactly the same way ?
It helps me a lot.
Many thanks!
I want to use more than 3 tabs and the tabs must be scrollable,,when i initialize more than 3,it gets all compressed and shown in a single page view….i want the tabs to be scrollable…What should i do??can u help me plz
Hi Ranjith,
Please use
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
to make your tabs scroll-able.-Thanks
Hey Mohit, just wanna thank you for an awesome tutorial!
Messing around with my first app right now, and this helped me out a lot!
Helpful!!! Thanks 🙂
Hi , Than you for amazing tutorial, But what if in Frag3 a button and when I click on it, will replace Frag3 with a new frag4
How to implemnt that please
Good Tutorial
Very elegant implementation of tabs, Mohit. However, I wasn’t able to build the project. In MainActivity.java, the line
final PagerAdapter adapter = new PagerAdapter (getSupportFragmentManager(), tabLayout.getTabCount());
is being flagged with an error: ‘PagerAdapter’ is abstract; cannot be instantiated. I’m building with Android Studio, 2.3.3. Thanks for your help. I’m eager to fold your code into my project.
Hi,
Thanks.
But I believe you may have imported the wrong class i.e. android.support.v4.view.PagerAdapter.
Instead you need to import the PagerAdapter class defined in the tutorial above.
-Mohit
Thank you for nice tutorial brother. I want to apply 3 tabs in the fragment how can I do that. I have 6 fragments and every fragment have three tabs can you help me how can I do that? I am confused because your are using tabs in activity but how can it be handle inside fragment
simply super sir. Sir I want the this tabbed activity after the login page. I tried a lot. The login data credentials are stored into the firebase when clicked on the login button but it doesn’t navigate to tabbed activity. It shows app is unfortunately stopped. Can u please help me out sir.
Good one, thanks mate.
Hi Mohit,
Thanks a lot for this useful tutorial. I am new to Android and I need one help with this tutorial.
I have implemented this code and it works fine. But I am facing a issue or blockage due to lack of knowledge here.
I have created 5 tabs and in each tabs I will show some data as list which will be fetched via REST API made by me. Now I know how to fetch but I dont know how show it like facebook or other apps as feed.
For 5 tabs I have created 5 classes extending Fragment. Do I need to write in each Class onCreateView method? Please help me as I am stuck and not getting any useful info. I tried a lot for 3 days but in vain.
Regards,
Chayan
yes
what a great tutorial and great site thanks guys
Thanks for the tutorial.
I want to navigate to 3rd tab while clicking an button in first tab. Please let me know how to do this?
Merci!
Great tutorial! Simple and easy to follow. How would you implement multiple fragments per tab?
Hey Mohit, Great work…
Can you please help, im using your code, with 6 tabs, an the problem i am facing is that, most of the times, there is a mismatch between the tab selected, and the corresponding viewpager…
any idea
Hi. I have a problem with this Tab system. The problem is how to change the text dynamically from MainActivity of tab1, tab2 or tab3,
I’m using inflater but no changes occurred due to program running.For example this code do not change the text into “JUST WE CAN”
View view = inflater.inflate(R.layout.coordinators_tab, container, false);
TextView txt = view.findViewById(R.id.textView4);
txt.setText(“JUST WE CAN”);
Bro thank you so much .. You rock ..:)
Thanks, Mohit, tut was very helpful.
Thanks for this awesome tutorial
how do i put the tabs in the navigation drawer items
Thanks for the nice tutorial, can u pls tell how to add “BackStack” in this for on back pressed?
Nice Tutorial dude,
anyway I created 3 bottom navigation with fragments.i want to add your tute into one of my bottom tab.It means I want to add 3 tabs into one of my bottom tab.How to add 3 tabs into bottom navigation…please answer and any tutorial link please…
Thanku so much…perfect and step by step tutorial.
Perfect tutorial! Congratulations. Thank you.
Mohit, I’m in grad school in a Mobile App Development course, and your help here was indispensible. Thank you, thank you, thank you. I was able to understand the structure of what you were doing, and apply that to my own design. Thanks for the crash course!
how it work if i combine the tab layout with navigation bottom view
thanks for the article
Thank you very much ..
but setOnTabSelectedListener is deprecated
I don’t know why !
Thank you very much Mr. Mohit. you saved my lot of time. its workng fine. Thanks again
Thanks. I implement this for my project and its work. Regards.