Question

Here is the code to setup the tabhost, however there are two problem

  1. The text will go to next line if it is too long, can I reduce the size and force it to single line?
  2. All icon do not show, even I am sure the image src is correct

    public class MainActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    
            tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
            tabHost.addTab(tabHost.newTabSpec("restaurant").setIndicator("Restaurant",getResources().getDrawable(R.drawable.food)),PlaceList.class, null);
            tabHost.addTab(tabHost.newTabSpec("attraction").setIndicator("Attraction",getResources().getDrawable(R.drawable.view)), PlaceList.class, null);
            tabHost.addTab(tabHost.newTabSpec("map").setIndicator("Map",getResources().getDrawable(R.drawable.map)),Map.class,null);
            tabHost.addTab(tabHost.newTabSpec("planner").setIndicator("Planner",getResources().getDrawable(R.drawable.plan)),Planner.class, null);
        }
     }
    
Was it helpful?

Solution

When calling TabSpec.setIndicator, the Drawable you pass in will only be visible if the label is null or empty. As far as making sure the TextView is restricted to a single line goes, you could loop over TabWidget.getTabCount, then call TabWidget.getChildTabViewAt and View.findViewById to get the TextView used to set the title. After that just make a call to TextView.setSingleLine.

    final TabWidget tabWidget = tabHost.getTabWidget();
    for (int i = 0; i < tabWidget.getTabCount(); i++) {
        final View tab = tabWidget.getChildTabViewAt(i);
        final TextView title = (TextView) tab.findViewById(android.R.id.title);
        title.setSingleLine();
    }

Alternatively, you could inflate your own tab layout by creating a style for Widget.TabWidget.

<style name="Your.TabWidget" parent="@android:style/Widget.TabWidget">
    <item name="android:tabLayout">@layout/your_tab_layout</item>
</style>

In your parent theme create a new item for android:tabWidgetStyle to apply it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top