Skip to content Skip to sidebar Skip to footer

How To Center Align Text In A Tab Bar In Android

I want to put only text in tab bar, no image... I want to center text in a tab bar, horizontally and vertically. Exactly in the center.

Solution 1:

If someone still interested in simple solution without creating own layout and styles:

  1. Use android.widget.TabHost.TabSpec#setIndicator(CharSequence label) for each added tab
  2. Use next code to center and wrap text inside the tab:

    inttabCount= tabHost.getTabWidget().getTabCount();
    for (inti=0; i < tabCount; i++) {
        finalViewview= tabHost.getTabWidget().getChildTabViewAt(i);
        if ( view != null ) {
            // reduce height of the tab
            view.getLayoutParams().height *= 0.66;
    
            //  get title text viewfinalViewtextView= view.findViewById(android.R.id.title);
            if ( textView instanceof TextView ) {
                // just in case check the type// center text
                ((TextView) textView).setGravity(Gravity.CENTER);
                // wrap text
                ((TextView) textView).setSingleLine(false);
    
                // explicitly set layout parameters
                textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
                textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
            }
        }
    }
    

Solution 2:

You can add this to xml layout android:gravity="center" or android:layout_gravity="center"

Solution 3:

<android.support.design.widget.TabLayout
app:tabMode="fixed"app:tabContentStart="0dp"

Solution 4:

Try this it will be help to you !!!

<?xml version="1.0" encoding="utf-8"?><TabHostxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/tabhost"android:layout_width="fill_parent"android:layout_height="wrap_content"android:padding="1dp" ><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="fill_parent"android:layout_height="60dp"android:layout_alignParentBottom="true" /><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="1dp" ></FrameLayout>

Post a Comment for "How To Center Align Text In A Tab Bar In Android"