qme
qme

Reputation: 391

How to align the actionbar tabs to the right?

I added the action bar tabs programmatically. I do not know how to align the action bar tabs to the right.

ActionBar bar = getActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // instantiate tabs
        ActionBar.Tab pageTab= bar.newTab().setText(getString(R.string.page))
                .setIcon(R.drawable.ic_page);
bar.addTab(pageTab);

        // other settings
        bar.setDisplayOptions(ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_TITLE);
        bar.setDisplayShowHomeEnabled(true);

        // remove the activity title to make space for tabs
        bar.setDisplayShowTitleEnabled(false);

Upvotes: 3

Views: 2861

Answers (3)

Goldorak84
Goldorak84

Reputation: 3972

I was able to align the actionbar right 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">right</item>
    </style>
</resources>

The tabs should now be right aligned.

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

Upvotes: -1

adrian5632
adrian5632

Reputation: 41

This has been a while since the original question, but... I needed a solution for the same problem due to visual appearance of the Activity. It turns out that if you append white spaces to either the title or subtitle, the tabs will be moved to the right, so you can do this:

bar.setTitle("Some titile\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");

This will shift the whole tab bar to accommodate the space for the white spaces. I must say that this is not an elegant solution but works if you don't have any actions in the action bar. (I didn't check what happens when actions are present.)

Upvotes: 2

CommonsWare
CommonsWare

Reputation: 1007464

Action bar tabs appear on the left. The right is used for your options menu and any items from it that you promote to be toolbar buttons.

Upvotes: 3

Related Questions