How to center text in tabhost?
Asked Answered
C

3

8

I have a basic question. In many tabhost examples, we find tabs with image and text.

In my case, I would like to only display a text, but the issue is that my text is horizontally centered but not vertically (The text is at the bottom of my tab).

I tried : android:layout_gravity="center" in the framelayout, but it doesn't work.

Do you have any idea, please ?

My xml.

<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center">

        <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_gravity="center"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center">

        </FrameLayout>

    </LinearLayout>

</TabHost>

solved : I customized my tabs thanks to the following tutorial : http://joshclemm.com/blog/?p=136

Thank you

Claudine answered 18/9, 2011 at 12:12 Comment(5)
The same question was asked a long time ago. Please read this issue enter link description hereElwandaelwee
Yes, I saw this post, but I still have the issue. :(Claudine
Its better to use a custom tab as default tabs has size of icons and text both.Aarhus
possible duplicate of How to center align text in a tab bar in AndroidThermolabile
Thank you.you're right. I customized my tabs thanks to the following tutorial : joshclemm.com/blog/?p=136Claudine
I
4
  1. Try to get the tab titles first and set gravity and layouts explicitly as shown below:

    TextView textView = (TextView)tabHost.getTabWidget().getChildAt(0)
    .findViewById(android.R.id.title);
    textView.setGravity(Gravity.CENTER);        
    textView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
    textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
    

    Hope this help.

Ingeminate answered 15/2, 2015 at 2:50 Comment(0)
P
0
android:layout_width="fill_parent"   
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal"
Pickwickian answered 18/9, 2011 at 12:23 Comment(1)
I tried your solution but it dosen't work. My text still is at the bottom of my tab. Does it work for you ?Claudine
P
0

Use the below to code to make tab text in center:

RelativeLayout.LayoutParams param=new

RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    param.addRule(RelativeLayout.CENTER_IN_PARENT);

TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0)
            .findViewById(android.R.id.title); // Unselected Tabs
    tv.setTextColor(Color.parseColor("#505050"));
    tv.setTextSize(10);
    tv.setLayoutParams(param); 
Pitchstone answered 20/10, 2014 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.