Set Title gravity to center in ActionBarSherlock

10,511

You have to set a custom layout for the Actionbar like this:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.yourlayout);

In your layout you can then add a TextView and set its gravity to center. E.g.:

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Header!"
        android:id="@+id/mytext"
        android:textColor="#ffffff"
        android:textSize="25dp" />

</LinearLayout>

Then you can set your custom typefont inside your Activity from your assets like this:

 TextView txt = (TextView) findViewById(R.id.mytext);  
 Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf");  
 txt.setTypeface(font);  
Share:
10,511

Related videos on Youtube

Ahmad
Author by

Ahmad

always happy to help

Updated on September 14, 2022

Comments

  • Ahmad
    Ahmad over 1 year

    Hello I would like to apply a custom Backgroundlayout just for my Main Activity. I couldn't find a solution. Can somebody give me some tips?

    would like to have just a simple TextView with a background in the Actionbar. Is this possible?

    I managed to remove the icon and set a backgroundimage. But how can I set the gravity of the text to center and change the typefont?

    getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(myimage)); 
    

    It should look like this: enter image description here

    thanks!

  • Adam
    Adam about 11 years
    If you have any tabs, this goes horribly wrong. The tabs remain, but the title goes BENEATH htem