Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize Android ActionBar to show a custom view before the tabs?

This seems to be the intended behaviour when using tabs and custom views. https://code.google.com/p/android/issues/detail?id=36191#c3

If you take a look at ActionBarSherlock - Tabs appearing ABOVE actionbar with custom view many other people are experiencing this as well and some people have offered solutions.

I have been unable to get any of the solutions working, but they may work for you. The trick seems to be to make sure the logo is is not set to be hidden. Call
getActionBar().setDisplayShowHomeEnabled(true) or getSupportActionBar().setDisplayShowHomeEnabled(true) if using ActionBarSherlock.

Then call:

View homeIcon = findViewById(
    Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
    android.R.id.home : R.id.abs__home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
((View) homeIcon).setVisibility(View.GONE);

This will get a reference to the actionbar view that holds the logo and sets it to gone with enables the custom view to fill the entire parent view, but should keep the tabs underneath...

As I said I was unable to get this working, but some people have had success.

Good luck.


I had the same problem and i figured out a way to solve it. It's not an "elegant" solution but it was the best i could find. As first thing since you want to modify the standard ActionBar behaviour you have to force ActionBar Sherlok to always use the non native implementation. To do that open ActionBarSherlok.java and comment this line of code:

registerImplementation(ActionBarSherlockNative.class);

then remove all the values-v11 values-v14 etc and be sure to always extend Theme.Sherlock and never Theme.Holo

At this point you are sure that the ActionBar implementation is always the one written by Jake Wharton. The only thing left to do is make the ActionBar view layout the way you want. Open ActionBarView.java and in the onLayout() method move this piece of code

 if (mExpandedActionView == null) { 
        final boolean showTitle = mTitleLayout != null && mTitleLayout.getVisibility() != GONE &&
                (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0;
        if (showTitle) {
            x += positionChild(mTitleLayout, x, y, contentHeight);
        }

        switch (mNavigationMode) {
            case ActionBar.NAVIGATION_MODE_STANDARD:
                break;
            case ActionBar.NAVIGATION_MODE_LIST:
                if (mListNavLayout != null) {
                    if (showTitle) x += mItemPadding;
                    x += positionChild(mListNavLayout, x, y, contentHeight) + mItemPadding;
                }
                break;
            case ActionBar.NAVIGATION_MODE_TABS:
                if (mTabScrollView != null) {
                    if (showTitle) x += mItemPadding;
                    x += positionChild(mTabScrollView, x, y, contentHeight) + mItemPadding;
                }
                break;
        }
    }

right before this piece

if (mProgressView != null) {
        mProgressView.bringToFront();
        final int halfProgressHeight = mProgressView.getMeasuredHeight() / 2;
        mProgressView.layout(mProgressBarPadding, -halfProgressHeight,
                mProgressBarPadding + mProgressView.getMeasuredWidth(), halfProgressHeight);
    }

You're done! Hope this helps