This Question have no Text answers yet!

How to make Andorid's ActionBar/TabWidget LinearLayout to wrap it's children

By : Lewapq
Source: Stackoverflow.com
Question!

I try to implement the scrollable tabs where each tab has it's size adjusted to it's content size.

I tried two approaches: ActionBar and TabHost. In both cases I was able to adjust tabs sizes.

To achieve that I setLayoutProperites on each Tab after given Tab was added. In both cases the same approach. The weight is changed to 0 and width is set to WRAP_CONTENT. Below the TabHost related example.

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabHost.getTabWidget().getChildTabViewAt(i).getLayoutParams();
        params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        params.weight=0;
        tabHost.getTabWidget().getChildTabViewAt(i).setLayoutParams(params);

I'm left with the issue related to the size of the LinearLayout (LL). The size of LL is greater that the sizes of all its children :

Tab sizes are as expected: Image-1

LL is marked and its size is to big(top), TabView is marked - it's size is correct (bottom): Image-2

The layout xml for TabHost case:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:orientation="vertical">

        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />
        </HorizontalScrollView>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <android.support.v4.view.ViewPager
            android:id="@+id/tabHost_viewpager"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</TabHost>

By : Lewapq


Video about How to make Andorid's ActionBar/TabWidget LinearLayout to wrap it's children