Markus
Markus

Reputation: 1482

Android Tablayout: 3 Tabs, let center tab take 70% of available width

I'm currently using a TabLayout in my application.

I set GRAVITY_FILL and MODE_FIXED but that does not accomplish what I want. I have 3 tabs all the time. The left and the right tab header only show a small Icon while the middle tab has a long text.

This is what I want:

[________TOTAL WIDTH__________]

[Icon][A very long tab header][Icon2]

This is how it looks like:

[__________________________TOTAL WIDTH__________________________]

[_________Icon________][A very long tab header][_________Icon2________]

So as you can hopefully see from my poor illustration I want to have the middle tab take about 70% of the space and give the rest to the 2 tabs with the icons.

Now I have already done some research and people suggest that I extend some of the layout classes but I could not get it to work.

Can someone please be so kind to let me know if this is possible and how I would achieve it.

Upvotes: 0

Views: 578

Answers (1)

Nitin Jain
Nitin Jain

Reputation: 1344

this is the only solution of your problem

    int padding_in_dp = 2;  // 6 dps
    final float scale = getResources().getDisplayMetrics().density;
    int padding_in_px = (int) (padding_in_dp * scale + 0.5f);
    for (int i = 0; i < 3; i++) {
        TabLayout.Tab tab = mTabLayout.newTab();
        mTabLayout.addTab(tab);
        LinearLayout layout = ((LinearLayout) ((LinearLayout) mTabLayout.getChildAt(0)).getChildAt(i));
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        layout.setLayoutParams(layoutParams);
        layout.setPadding(padding_in_px, 0, padding_in_px, 0);
        tab.setText(array[i]);
    }

It will surely help you

Upvotes: 1

Related Questions