7

I'm going to create an application which is designed to use Tabhost, but as I know it's been deprecated. So my question is should I use Tabhost anyway or how can I replace it..by using buttons instead of them declared in every xml or any other suggestions?

manlio
  • 18,345
  • 14
  • 76
  • 126
Android-Droid
  • 14,365
  • 41
  • 114
  • 185

3 Answers3

12

"Deprecated" in Android means "we think there is a better solution that you should investigate". Rarely does "deprecated" mean "it is unusable". TabHost, AFAIK, works fine on Android 4.0.

That being said, I would recommend considering switching to tabs in the action bar, using ActionBarSherlock to give you backwards compatibility to Android 2.1.

UPDATE

Besides, TabHost is not deprecated. TabActivity is deprecated. You can still use TabHost, with views for your tabs. Or, use:

  • ViewPager with a tabbed indicator, like PagerTabStrip
  • FragmentTabHost, for a TabHost that uses fragments for tabs

The action bar also has tab support, but that was deprecated starting with the "L" Developer Preview.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm asking because I think I read somewhere that it's better to use fragments instead of tabhost. – Android-Droid Apr 24 '12 at 12:17
  • @Bombastic: A common pattern of using tabs in the action bar is to have clicking a tab do a `FragmentTransaction` to replace a fragment on the screen. You do not have to use fragments to use tabs in the action bar, though. – CommonsWare Apr 24 '12 at 12:27
  • 1
    Thanks for the answer! I guess I'll use `ActionBarSherlock` for my application : ) – Android-Droid Apr 24 '12 at 12:30
  • 1
    Is TabHost really deprecated? I think the TabActivity is deprecated. The whole tab mechanism is absolutely confusing. It would be nice if there could be one consistent mechanism supported by the ActionBar including the left/right flick. Does someone know suggested samples that implement it the way it should be? I have read through samples provided by the ActionBarSherlock library that use the above mentioned TabHost all over the place... – denis Apr 26 '12 at 11:02
  • 2
    @and-dev: You are correct that `TabHost` itself is not deprecated -- I was not paying close enough attention with my answer and was following along with the phrasing used in the question. `TabActivity` is formally deprecated, mostly because `ActivityGroup` is deprecated, in favor of fragments. Tabs in the action bar, however, is still an alternative to using `TabHost`. My apologies for the confusion. – CommonsWare Apr 26 '12 at 11:32
  • @CommonsWare Thank you for clearing up the confusion. Which one is the recommended way of implementing static tabs (like seen in the YouTube app or the ICS Dialer) that allow to slide/flick from the right to the left? (which is one of the suggested navigation mechanisms mentioned in the Android design guidelines) – denis Apr 26 '12 at 11:40
  • 1
    @and-dev: Use a `ViewPager` for the body for the swiping behavior. In terms of the tabs, use the action bar (see `ViewPager` JavaDocs), or use a third-party implementation of "swipey tabs" like http://viewpagerindicator.com/ or https://github.com/astuetz/android-viewpagertabs – CommonsWare Apr 26 '12 at 11:51
  • @CommonsWare One last question. Is it correct that the ActionBar tab mechanism is only able to work with Fragments or is there a possibility to switch between Activities as well? (it is mentioned right here http://developer.android.com/guide/topics/ui/actionbar.html#Tabs) Or is it recommended to use the tabs with Fragments only? – denis Apr 26 '12 at 12:24
  • 1
    @and-dev: Activities-in-tabs is specifically what was deprecated (`ActivityGroup`). – CommonsWare Apr 26 '12 at 12:47
3

As per Android API level-18, ActionBarSherlock is not recommended instead of that,they have introduced ActionBarCompat .. Better to go with ActionBarCompat.

Thank you.

Gowtham Kumar
  • 534
  • 8
  • 22
2

TabHost was deprecated in API level 30. You can use material TabLayout and ViewPager instead of TabHost:

<com.google.android.material.tabs.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="center"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabIndicatorHeight="2dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/colorPrimary"
            app:tabTextAppearance="@style/MapTabTextStyle"
            app:tabTextColor="@color/black" />

code:

tablayout.addTab(tablayout.newTab().setText("Tab1"))
tablayout.addTab(tablayout.newTab().setText("Tab2"))
tablayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {

        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
            
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            // do something
        }

    })
Kuvonchbek Yakubov
  • 558
  • 1
  • 6
  • 16