14

I want to make vertical tabs in Android like below image. enter image description here

I had see example for vertical tabs from below link.

Click here

In this link answer has some comments and in comments they had share code but the link of mega-upload is expired.

I had try many ways but not able to display tabs vertical. When I am trying according to link the tabs can not be display. Please help me

Community
  • 1
  • 1
Dharmendra
  • 33,296
  • 22
  • 86
  • 129

1 Answers1

12

When I use tabs, I normally just hide the tabwidget tag by setting android visibility as gone.

And add buttons to act as the tab buttons like

THIS IS MODIFIED TO MAKE VERTICAL TAB BUTTONS

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:orientation="horizontal" 
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <FrameLayout android:layout_width="0dip" 
            android:layout_height="fill_parent" android:layout_weight="0.2">
        <TabWidget android:id="@android:id/tabs" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:visibility="gone"/>
            <LinearLayout android:layout_width="fill_parent"  
                android:layout_height="fill_parent"
                android:orientation="vertical">
                <Button android:layout_height="0dip" 
                    android:layout_width="fill_parent" 
                    android:layout_weight="1.0"
                android:background="@drawable/ic_tab_artists"               
                    android:id="@+id/artist_id" 
                    android:onClick="tabHandler"/>
                <Button android:layout_height="0dip" 
                    android:layout_width="fill_parent" 
                    android:layout_weight="1.0"
                android:background="@drawable/ic_tab_artists"  
                    android:id="@+id/album_id" 
                    android:onClick="tabHandler"/>
                <Button android:layout_height="0dip" 
                    android:layout_width="fill_parent" 
                    android:layout_weight="1.0"
                android:background="@drawable/ic_tab_artists"   
                    android:id="@+id/song_id" 
                    android:onClick="tabHandler"/>
        </LinearLayout> 
    </FrameLayout>       
    <FrameLayout android:id="@android:id/tabcontent" 
        android:layout_width="0dip" 
        android:layout_height="fill_parent" android:layout_weight="0.8"/>
</LinearLayout>

and I add a button click handler

public void tabHandler(View target){
    artistButton.setSelected(false);
    albumButton.setSelected(false);
    songButton.setSelected(false);
    if(target.getId() == R.id.artist_id){
        tabHost.setCurrentTab(0);
        artistButton.setSelected(true);
    } else if(target.getId() == R.id.album_id){
        tabHost.setCurrentTab(1);
        albumButton.setSelected(true);
    } else if(target.getId() == R.id.song_id){
        tabHost.setCurrentTab(2);
        songButton.setSelected(true);
    }
}

When I use this method, it gives me more freedom to style the tab buttons. The above xml is for horizontal tab buttons but you easily make it vertical but editing it a bit. Just make sure you need the Tahbost,Tabwidget and a framelayout with @android:id/tabcontent as the id.

blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • Thanks for your reply.I can able to display tabs in horizontal but I am not able to display tab in vertical.If you have any idea about it please help me – Dharmendra Sep 01 '11 at 09:17
  • Yes this is the alternative option of vertical tab. thanks for your answer.I am looking for inbuilt functionality like the link in my question. – Dharmendra Sep 02 '11 at 03:40
  • I keep getting errors on the button handler code. any ideas why – mr.j05hua May 24 '12 at 00:00
  • I keep getting forcecloses error on start up but no errors on compiling. The only issue I can think is that I have forced it into landscape. would that cause any issues. – mr.j05hua May 25 '12 at 12:24
  • 1
    Make it a separate question with the error logs. you will get more response. – blessanm86 May 28 '12 at 05:07
  • How did you setup tabhost? I'm trying to use findView(android.R.id.tabhost) then tabhost.setup(), but tabhost currenttab is always -1. – Felipe Centeno Oct 23 '17 at 22:46
  • TabHost is deprecated in API 30 https://developer.android.com/reference/android/widget/TabHost – Tushski Aug 09 '23 at 07:35