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.
How to center align text in a tab bar in Android
You can add this to xml layout android:gravity="center"
or android:layout_gravity="center"
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 TabLayout –
Canard
If someone still interested in simple solution without creating own layout and styles:
- Use android.widget.TabHost.TabSpec#setIndicator(CharSequence label) for each added tab
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; } } }
You can add this to xml layout android:gravity="center"
or android:layout_gravity="center"
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 TabLayout –
Canard
<android.support.design.widget.TabLayout
app:tabMode="fixed"
app:tabContentStart="0dp"
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>
You can add the following line to the Tab layout to align the text to the center.
app:tabGravity="fill"
© 2022 - 2024 — McMap. All rights reserved.