Search code examples
androidandroid-actionbar

How can I make action bar tabs center align in Android?


Android action bar tabs are left-aligned by default, I want to align it the center of the action bar. Is there any way we can make it center?


Solution

  • I was able to center-align the actionbar by customizing my application theme

    In my AndroidManifest.xml, I added a android:theme property:

    <application
            android:label="@string/app_name"
            android:icon="@drawable/ic_launcher"
            android:theme="@style/CustomActionBarTheme">
      ...
    

    I then added a new themes.xml file to my project, and added :

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- the theme applied to the application or activity -->
        <style name="CustomActionBarTheme"
               parent="@android:style/Theme.Holo.Light.DarkActionBar">
            <item name="android:actionBarTabBarStyle">@style/MyActionBarTabBar</item>
        </style>
    
        <style name="MyActionBarTabBar">
            <item name="android:gravity">center</item>
        </style>
    </resources>
    

    The tabs should now be center-aligned.

    EDIT: It works only if your actionbar is displayed on 2 lines (in portrait)