Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center align text in a tab bar in Android

Tags:

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.

like image 324
sam_k Avatar asked Mar 02 '11 07:03

sam_k


People also ask

How do you center text on a tab?

Select the text that you want to center. in the Page Setup group, and then click the Layout tab. In the Vertical alignment box, click Center. In the Apply to box, click Selected text, and then click OK.

How do you center text on android?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

How do I align text to the center?

Center Align Text To just center the text inside an element, use text-align: center; This text is centered.

How do you center text in the middle of the page on Google Docs?

To center text on a page, drag your cursor through the text you want to center, click on the align icon in the action bar (to the left of the line-spacing icon), and select "center align" (the second option from the left).


2 Answers

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:

    int tabCount = tabHost.getTabWidget().getTabCount();
    for (int i = 0; i < tabCount; i++) {
        final View view = tabHost.getTabWidget().getChildTabViewAt(i);
        if ( view != null ) {
            // reduce height of the tab
            view.getLayoutParams().height *= 0.66;
    
            //  get title text view
            final View textView = 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;
            }
        }
    }
    
like image 126
se.solovyev Avatar answered Sep 17 '22 09:09

se.solovyev


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

like image 30
iamtheexp01 Avatar answered Sep 20 '22 09:09

iamtheexp01