Android: Tabs at the BOTTOM
Asked Answered
C

10

149

I've seen some chatter about this, but nothing definite. Is there a way to put the tabs in a TabWidget to the bottom of the screen? If so, how?

I've tried the following, but didn't work:

a) setting the tabwidget below the framelayout
b) setting the tabwidget's gravity to "bottom"

Thanks! llappall

Charlie answered 7/3, 2010 at 8:40 Comment(3)
Please define "didn't work".Limerick
I use one from the below site. it's working http://kpbird.blogspot.com/2011/05/androidbottom-tabbar-control.htmlProvidenciaprovident
Look here if you want iPhone like tab hosts.Theatre
B
272

Here's the simplest, most robust, and scalable solution to get tabs on the bottom of the screen.

  1. In your vertical LinearLayout, put the FrameLayout above the TabWidget
  2. Set layout_height to wrap_content on both FrameLayout and TabWidget
  3. Set FrameLayout's android:layout_weight="1"
  4. Set TabWidget's android:layout_weight="0" (0 is default, but for emphasis, readability, etc)
  5. Set TabWidget's android:layout_marginBottom="-4dp" (to remove the bottom divider)

Full code:

<?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="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:layout_weight="1"/>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:layout_marginBottom="-4dp"/>

    </LinearLayout>

</TabHost>
Bullfight answered 25/4, 2010 at 23:40 Comment(13)
The divider is actually hard coded in. I was trying to change the drawables used for the tabs and found it. Total bummer.Decent
You actually found where it's coded in the Widget, and there's NO interface to change it? Surprising and disappointing. So currently the only solution is manually drawing a divider above it, and having the line below the tabs remain...Bullfight
My approach is a simple realignment of the tabs to the bottom. For a custom TabWidget with this and other features, it looks like this guy made one that supports tab vertical alignment, as well as some other customizations such as tab backgrounds / icons.Bullfight
You should select this (or something) as the accepted answer.Pyrogallate
The only required deviation here from Google's standard TabWidget example is setting layout_weight=1 on the FrameLayout. This allows the tab control to "claim" its height out of the LinearLayout first.Topazolite
To anyone not able to get this just right, I had to add android:layout_gravity="bottom" to the TabWidget to get this to work in 2.3.3. +1, thanks so much for this.Avocet
In order to get rid of the divider at the bottom of the TabWidget, simply add android:layout_marginBottom="-5px" to the TabWidget. This will move the TabWidget off the bottom of the screen 5 pixels, just enough so you don't see the divider. The FrameLayout will compensate in size so it works out perfectly. I'm not sure if the size of the divider changes on larger devices or not. You might have to use dp instead of px.Abernethy
This is showing only one tab at the bottom. I don't know the reason may be other tabs are overlapping.Please Tell me what is the problem ?Contaminate
Worked for me. Thanks @Bullfight and Jakobud .. I have up voted your answers. Thanks for the solution..Dubose
I've incorporated the remove-divider comment into the answer.Keelia
That's the solution I came up with, but upon focus of an EditText, the TabWidget moves up. Any possible fix for that?Doralynne
[This solution] (https://mcmap.net/q/160500/-android-keyboard-puts-tab-bar-on-top) worked for me (for the problem I asked previously).Doralynne
how can you accomplish this by using the actionbar.NAVIGATION_MODE_TABS?Wite
N
38

Try it ;) Just watch the content of the FrameLayout(@id/tabcontent), because I don't know how it will handle in case of scrolling... In my case it works because I used ListView as the content of my tabs. :) Hope it helps.

<?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">
    <RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        <FrameLayout android:id="@android:id/tabcontent"
             android:layout_width="fill_parent" 
             android:layout_height="fill_parent"
             android:layout_alignParentTop="true" 
             android:layout_above="@android:id/tabs" />
    <TabWidget android:id="@android:id/tabs"
             android:layout_width="fill_parent" 
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true" />
    </RelativeLayout>
</TabHost>
Nathalie answered 20/8, 2010 at 18:31 Comment(2)
+1 But did you notice the black/gray bar on top of the screen? How did you remove it?Sardonic
I am using this to post the tab widget onto the left side of the form.Luminiferous
L
11

There is a way to remove the line.

1) Follow this tutorial: android-tabs-with-fragments

2) Then apply the RelativeLayout change that Leaudro suggested above (apply the layout props to all FrameLayouts).

You can also add an ImageView to the tab.xml in item #1 and get a very iPhone like look to the tabs.

Here is a screenshot of what I'm working on right now. I still have some work to do, mainly make a selector for the icons and ensure equal horizontal distribution, but you get the idea. In my case, I'm using fragments, but the same principals should apply to a standard tab view.

enter image description here

Laird answered 2/11, 2011 at 2:42 Comment(2)
Its been a while since you posted this, but your link now says Welcome to nginx! and nothing else is there on page.Lactose
It wasn't my site, sop I don't have control over it unfortunately. After this amount of time though, the systems have changed enough that you likely shouldn't be implementing this kind of interface for your Android users, they would expect things to work a little differently now.Laird
C
3

For all those of you that try to remove the separating line of the tabWidget, here is an example project (and its respective tutorial), that work great for customizing the tabs and thus removing problems when tabs are at bottom. Eclipse Project: android-custom-tabs ; Original explanation: blog; Hope this helped.

Ciliary answered 13/4, 2011 at 13:4 Comment(0)
W
3

Not sure if it will work for all versions of Android (especially those with custom UI stuff), but I was able to remove the gray bar at the bottom by adding

 android:layout_marginBottom="-3dp"

to the TabWidget XML...

Wellspring answered 29/7, 2011 at 23:52 Comment(0)
X
3

There are two ways to display tabs at the bottom of a tab activity.

  1. Using relative layout
  2. Using Layout_weight attribute

Please check the link for more details.

Xylo answered 20/9, 2011 at 16:31 Comment(0)
A
1

This may not be exactly what you're looking for (it's not an "easy" solution to send your Tabs to the bottom of the screen) but is nevertheless an interesting alternative solution I would like to flag to you :

ScrollableTabHost is designed to behave like TabHost, but with an additional scrollview to fit more items ...

maybe digging into this open-source project you'll find an answer to your question. If I see anything easier I'll come back to you.

Abbie answered 7/3, 2010 at 18:12 Comment(0)
C
1

Yes, see: link, but he used xml layouts, not activities to create new tab, so put his xml code (set paddingTop for FrameLayout - 0px) and then write the code:

public class SomeActivity extends ActivityGroup {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

    TabHost tab_host = (TabHost) findViewById(R.id.edit_item_tab_host); 

    tab_host.setup(this.getLocalActivityManager());

    TabSpec ts1 = tab_host.newTabSpec("TAB_DATE"); 
    ts1.setIndicator("tab1"); 
    ts1.setContent(new Intent(this, Registration.class)); 
    tab_host.addTab(ts1); 

    TabSpec ts2 = tab_host.newTabSpec("TAB_GEO"); 
    ts2.setIndicator("tab2"); 
    ts2.setContent(new Intent(this, Login.class)); 
    tab_host.addTab(ts2); 

    TabSpec ts3 = tab_host.newTabSpec("TAB_TEXT"); 
    ts3.setIndicator("tab3"); 
    ts3.setContent(new Intent(this, Registration.class)); 
    tab_host.addTab(ts3); 

    tab_host.setCurrentTab(0);      


}

}

Conclave answered 30/3, 2010 at 5:44 Comment(0)
N
1

I was having the same problem with android tabs when trying to place them on the bottom of the screen. My scenario was to not use a layout file and create the tabs in code, I was also looking to fire activities from each tab which seemed a bit too complex using other approaches so, here is the sample code to overcome the problem:

adding-tabs-in-android-and-placing-them

Nesta answered 29/1, 2011 at 18:29 Comment(0)
C
0

I recomend use this code for stable work, it optimized for nested fragments in tab (for example nested MapFragment) and tested on "do not keep activities": https://mcmap.net/q/160501/-set-actionbar-tabs-at-bottom-in-actionbaractivity

Checklist answered 18/4, 2014 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.