How to center align text in a tab bar in Android
Asked Answered
M

5

9

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.

Mariel answered 2/3, 2011 at 7:10 Comment(1)
so give me good answer..Mariel
P
2

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

Personalize answered 2/3, 2011 at 7:14 Comment(2)
If you add the android:layout_gravity="center", the parent layout will be centered but if you use android:layout_gravity="center", the child views will be centered. Child views are the element inside your layout. For example, linear layout is your parent while inside linear layout you define child views such as button and textview.Personalize
@exception where to add this android:layout_gravity="center" ? to TabLayout? It's not working for me, can you plz reply. Also I cant give 'android:gravity' to TabLayoutCanard
J
32

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;
            }
        }
    }
    
Jawbone answered 21/12, 2011 at 21:42 Comment(0)
P
2

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

Personalize answered 2/3, 2011 at 7:14 Comment(2)
If you add the android:layout_gravity="center", the parent layout will be centered but if you use android:layout_gravity="center", the child views will be centered. Child views are the element inside your layout. For example, linear layout is your parent while inside linear layout you define child views such as button and textview.Personalize
@exception where to add this android:layout_gravity="center" ? to TabLayout? It's not working for me, can you plz reply. Also I cant give 'android:gravity' to TabLayoutCanard
P
1
<android.support.design.widget.TabLayout
app:tabMode="fixed"
app:tabContentStart="0dp" 
Protuberance answered 7/3, 2017 at 21:24 Comment(0)
M
0

Try this it will be help to you !!!

<?xml version="1.0" encoding="utf-8"?>
 <TabHost xmlns: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" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true" />

<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="1dp" >
</FrameLayout>
Modie answered 22/6, 2012 at 11:42 Comment(0)
U
0

You can add the following line to the Tab layout to align the text to the center.

                           app:tabGravity="fill"
Urticaria answered 4/7, 2022 at 7:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.