Should I use ActionBar tabs or TabLayout if my target android version is 4.2? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Should I use ActionBar tabs or TabLayout if my target android version is 4.2? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m making my first android app and I want it to have an action bar at the top and tabs right below the action bar so that I can swipe between different fragments. Pretty much exactly like the current whatsapp or youtube apps.

I did a little googling and found this tutorial. Then I noticed it says “As of Android 5.0, ActionBar Tabs is now officially deprecated. Tabs should now be built using the TabLayout.” and pointed to a newer tutorial.

What confuses me is the mention of “Android 5.0”. Are they trying to say that if you’re developing for 5.0, you should use TabLayout, or that if you’re developing after the time that 5.0 was released you should be using TabLayout regardless of what is your target API? As I mentioned in the title, my phone runs 4.2, and I’m just making this app for myself, so of course I want it running on my version of android.

So what should I do? Should use the deprecated ActionBar Tabs to ensure it works on my phone, or will the new TabLayout work out as well? Or maybe I should use support libraries or something? For example will this thing help?

I guess I should mention that while this is my first ever proper android app that I’m making, I am already familiar with some of the basic concepts. So be detailed in your explanations, but no need to explain stuff like what an activity is :). Though I’ll probably ask for clarification anyway if something’s unclear.

Answer

You can use android design support library that includes TabLayout and is supported on Android lower versions

With a little help from the new Android Design Support Library,Android Design Support Library, we’re bringing a number of important material design components to all developers and to all Android 2.1 or higher devices.

Here’s what it suggests

The Design library’s TabLayout implements both fixed tabs, where the view’s width is divided equally between all of the tabs, as well as scrollable tabs, where the tabs are not a uniform size and can scroll horizontally. Tabs can be added programmatically:

TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));

However, if you are using a ViewPager for horizontal paging between tabs, you can create tabs directly from your PagerAdapter’s getPageTitle() and then connect the two together using setupWithViewPager(). This ensures that tab selection events update the ViewPager and page changes update the selected tab.

We are here to answer your question about Should I use ActionBar tabs or TabLayout if my target android version is 4.2? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji