6

I have an application with 4 tabs. By default every tab width is 1/4 of the screen width. How can I override this?

I need the tabs to have a different width for each one. Any ideas on how to accomplish that?

momo
  • 106
  • 1
  • 5
  • i am also looking for the same, checkout this: http://stackoverflow.com/questions/3446722/android-tabhost-problem-setindicator – Paresh Mayani Aug 25 '10 at 04:59
  • Looks interesting Paresh. Did you try any of both proposed solutions (Style or overriding TabHost)? Right now I'm looking at this issue and thinking the best/fastest approach is to make our own Tabs (with buttons and listeners). – momo Aug 25 '10 at 09:24

4 Answers4

2

Try this:

tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 35; 
Zeba
  • 3,041
  • 1
  • 28
  • 39
  • This definitely changes the height of tab but your text will go missing ... pls let me knw if u have solution for this issue . – Code_Life Aug 09 '12 at 11:47
  • Your text will go missing only if your text size and tab height are not in proportion. Try reducing tab text size or increase the height of the tab so that the text is seen. – Zeba Aug 16 '12 at 13:17
1

Finally I managed to have a different sized tabs using ToggleButton.

My XML is like this

<LinearLayout
  android:id="@+id/tabs"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_below="@id/header"
>

  <ToggleButton
  android:id="@+id/tab1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:drawableRight="@drawable/icon_home"
    android:textOn=""
    android:textOff=""
    android:background="@drawable/tabselector"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    android:layout_weight="0"
    />
        <ImageView
    android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/top_menubar_separator" />

<ToggleButton
  android:id="@+id/tab2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
    android:drawableRight="@drawable/icon_store"
    android:textOn="Store"
    android:textOff="Store"
    android:background="@drawable/tabselector"
    android:paddingLeft="15dip"
    android:paddingRight="15dip"
    android:layout_weight="2"
    />

Where each ToggleButton is a tab. The trick here is using the weight to expand the tabs to completely fill the screen.

The teab_selector.xml looks like this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item   android:drawable="@drawable/top_menubar_selected"
            android:state_pressed="true"
           />
 <item  android:drawable="@drawable/top_menubar_selected"
            android:state_checked="true"
           />
      <item     android:drawable="@drawable/top_menubar_1px" />
</selector>

I still need to code the logic for un-press a button when a new one is selected and launch an intent with ta activity, but looks nice so far.

momo
  • 106
  • 1
  • 5
0

Try this:

<TabWidget android:layout_width="wrap_content" ... />
plugmind
  • 7,926
  • 4
  • 34
  • 39
  • Hi radek-k, I'm already using "wrap_content" and makes no difference if I change it. Probably is due I'm using a View as Tab Indicator, not just text. Thanks anyway – momo Aug 25 '10 at 09:17
0

For the sake of someone still using FragmentTabHost and TabWidget, getting the tab to wrap to the text and not use a within-word break strategy can be achieved using a PreDrawListener on the ViewTreeObserver of the tab indicator view before setting the text in it. Something like this

    final View newTabView = LayoutInflater.from(this).inflate(R.layout.tab_view, null);
    mTabHost.addTab(mTabHost.newTabSpec(index + "").setIndicator(newTabView),
            YourClassArgument.class, null);
    final TextView titleTextView = newTabView.findViewById(R.id.tabTextView);
    titleTextView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            titleTextView.getViewTreeObserver().removeOnPreDrawListener(this);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) newTabView.getLayoutParams();
            layoutParams.width = (int) Math.ceil(titleTextView.getLayout().getLineWidth(0))
                    + newTabView.getPaddingLeft() + newTabView.getPaddingRight();
            newTabView.setLayoutParams(layoutParams);
            return false;
        }
    });
    titleTextView.setText(tabTitle);
AA_PV
  • 1,339
  • 1
  • 16
  • 24