Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create tabbed layout in Android 4.0?

Now that TabActivity is deprecated, what is the proper way to build a tabbed interface using fragments? Every tutorial I have seen takes a different approach. Some of them are supremely complicated pushing a mundane programming topic to the rocket science territory. What gives?

I am coding for ICS Android 4.0.

Thanks.

like image 384
RajV Avatar asked Nov 08 '11 16:11

RajV


People also ask

How do I create a tab layout?

Tabs are created using newTab() method of TabLayout class. The title and icon of Tabs are set through setText(int) and setIcon(int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab(Tab) method.

How do I use ViewPager on Android?

Implement Swipe Views You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .


1 Answers

The preferred way now is to use the tabs that ActionBar gives you. It's quite easy to set up, given that you like this option. With ActionbarSherlock, you can even port your app all the way back to 1.6.

It's not suitable for every case however. For example, you might need those tabs to be inside a fragment, on a screen that displays multiple fragments at once. In that case, you have to stick with the "deprecated" methods to set up the tabs, using a TabWidget. It's ugly, but possible.

Edit:

Check Jeff's comment. As he pointed out, I was wrong, TabWidget is still perfectly valid, TabAvtivity is the deprecated class here!

like image 75
Zsombor Erdődy-Nagy Avatar answered Oct 12 '22 22:10

Zsombor Erdődy-Nagy