How to Customize ActionBar Title in Android

338 Views Asked by At

I have the following code:

viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabPagerAdapter(getSupportFragmentManager(), myAccount);

viewPager.setAdapter(mAdapter);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.actionbar);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// ADD TABS
for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText(tab_name)
            .setTabListener(this));
}

And this is the result of the above code:

Android actionbar title

What i would like to do is to have my title to be placed above my tabs instead of the other way around as seen in the picture. How do i do this?

* EDIT 1 *

I can achieve this with the following code (not using custom layout)

viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabPagerAdapter(getSupportFragmentManager(), myAccount);

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// ADD TABS
for (String tab_name : tabs) {
    actionBar.addTab(actionBar.newTab().setText(tab_name)
            .setTabListener(this));
}

Here is the screenshot:

Without using custom layout

I am trying to achieve this but using custom layout

1

There are 1 best solutions below

3
On
private String[] tabs = { "English", "Tamil", "Hindi" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

They passing string array into tabs, like here tamil, english, hindi. Change that to watever you want.