Lalit Sharma
Lalit Sharma

Reputation: 1150

How to set Tab divider in custom Tab Widget in android

How to set custom or default tab divider in tab widget. I have tried below code to create custom tab widget.

private void setTabs()
{
    TabHost tabhost=getTabHost();

    tabhost.getTabWidget().setDividerDrawable(TabWidget.SHOW_DIVIDER_MIDDLE);//here Application crashes

    tabhost.addTab(createTab(FoodTabGroup.class, "foods", "Foods", R.drawable.tab_foods));
    tabhost.addTab(createTab(BeveragesTabGroup.class, "beverages", "Beverages", R.drawable.tab_beverages));
    tabhost.addTab(createTab(DessertsTabGroup.class, "desserts", "Desserts", R.drawable.tab_desserts));
    tabhost.addTab(createTab(WinesTabGroup.class, "wines", "Wines", R.drawable.tab_wines));
    tabhost.addTab(createTab(OrderTabGroup.class, "order", "Order", R.drawable.tab_order));

    tabhost.getTabWidget().getChildAt(0).getLayoutParams().width = 140;
    tabhost.getTabWidget().getChildAt(1).getLayoutParams().width = 140;
    tabhost.getTabWidget().getChildAt(2).getLayoutParams().width = 140;
    tabhost.getTabWidget().getChildAt(3).getLayoutParams().width = 140;
    tabhost.getTabWidget().getChildAt(4).getLayoutParams().width = 140;

    tabhost.getTabWidget().getChildAt(0).getLayoutParams().height= 150;
    tabhost.getTabWidget().getChildAt(1).getLayoutParams().height= 150;
    tabhost.getTabWidget().getChildAt(2).getLayoutParams().height= 150;
    tabhost.getTabWidget().getChildAt(3).getLayoutParams().height= 150;
    tabhost.getTabWidget().getChildAt(4).getLayoutParams().height= 150;

    tabhost.setCurrentTab(0);
}

private TabSpec createTab(final Class<?> intentClass, final String tag, 
        final String title, final int drawable)
{
    final Intent intent = new Intent();
    intent.setClass(this, intentClass);

    final View tab = LayoutInflater.from(getTabHost().getContext()).
        inflate(R.layout.tab, null);
    TextView txtTab=(TextView)tab.findViewById(R.id.tab_text);
    txtTab.setText(title);
    txtTab.setPadding(8, 9, 8, 9);
    txtTab.setTextColor(Color.WHITE);
    txtTab.setTextSize(18);

    ImageView imgTab=(ImageView)tab.findViewById(R.id.tab_icon);
    imgTab.getLayoutParams().height=80;
    imgTab.getLayoutParams().width=80;
    imgTab.setImageResource(drawable);

    return getTabHost().newTabSpec(tag).setIndicator(tab).setContent(intent);
}

Please help me for setting custom divider in tab widget..

Thanks in advance.

Upvotes: 1

Views: 7997

Answers (2)

malavika
malavika

Reputation: 1331

Try with this.

    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);

Upvotes: 1

Matthew Pape
Matthew Pape

Reputation: 1691

What version are you trying to run on?

setDividerDrawable() takes the id of the drawable you want to use for your dividers. The argument you are passing is actually used in conjunction with setShowDividers(), which tells it where to draw the dividers.

Replace...

tabhost.getTabWidget().setDividerDrawable(TabWidget.SHOW_DIVIDER_MIDDLE);

with...

tabhost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);
tabhost.getTabWidget().setDividerDrawable( ... ); //id of your drawble resource here

Upvotes: 5

Related Questions