0

I'm trying since several hours to center my title in the action bar horizontally. This is my code:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/actionBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false"
            android:layout_gravity="center"
            android:gravity="center">

            <TextView 
                 android:id="@+id/activityTitle"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:textSize="20sp"
                android:textColor="#FFFFFF" />
    </LinearLayout>

and this is my onCreate() code in my activity:

        this.getActionBar().setDisplayShowCustomEnabled(true);
        this.getActionBar().setDisplayShowTitleEnabled(false);

        LayoutInflater inflator = LayoutInflater.from(this);
        View v = inflator.inflate(R.layout.custom_action_bar, null);

        Typeface tf = Typeface.createFromAsset(getAssets(),"font/myfont.ttf");
        ((TextView)v.findViewById(R.id.activityTitle)).setTypeface(tf);
        ((TextView)v.findViewById(R.id.activityTitle)).setText("Test");

        getActionBar().setCustomView(v);
        getActionBar().setDisplayShowHomeEnabled(false);

The actionbar icon is hided and the title in the correct font, but it is only centered vertically, but not horizontally. How can I do that?

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Maddy
  • 570
  • 1
  • 7
  • 27

1 Answers1

0

You could use a RelativeLayout and the properties

android:layout_centerHorizontal="true"    

and

android:layout_centerVertical="true"

for example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/actionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/activityTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Jorgesys was here!"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />

</RelativeLayout>
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • Sorry, that doesn't work. The text is only centered, if I delete my java code and took only your code – Maddy Mar 21 '15 at 09:41