Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarSherlock - Tabs appearing ABOVE actionbar with custom view

I Am trying to create an actionbar without the app logo/text, and with a centralised picture, and i know it is possible with a custom view, here is my code:

protected void initActionBar()
{
    RelativeLayout custom = new RelativeLayout(getApplicationContext());
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    custom.setLayoutParams(params); 

    custom.setGravity(Gravity.CENTER);
    custom.setBackgroundDrawable(getResources().getDrawable(R.drawable.background_grad_grey_rounded));

    ImageView image =new ImageView(getApplicationContext());
    image.setBackgroundResource(R.drawable.ic_launcher); 
    custom.addView(image);

    ab = getSupportActionBar();


    ab.setDisplayShowCustomEnabled(true);
    ab.setCustomView(custom);
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    tab_connect = ab.newTab();
    tab_connect.setText("CONNECT");
    tab_connect.setTabListener(this);

    tab_discover = ab.newTab();
    tab_discover.setText("DISCOVER");
    tab_discover.setTabListener(this);

    tab_simplify= ab.newTab();
    tab_simplify.setText("SIMPLIFY");
    tab_simplify.setTabListener(this);

    ab.addTab(tab_simplify);
    ab.addTab(tab_discover);
    ab.addTab(tab_connect);

    ab.setDisplayShowTitleEnabled(false);
    ab.setDisplayShowHomeEnabled(false);

}

however when i hide the logo, the actionbar shifts below my tabs like so:

screen shot

but if i set ab.setDisplayShowHomeEnabled(true) the actionbar appears in it's right place (but with the logo which i don't want):

enter image description here

What am i doing wrong?

like image 230
AndroidNoob Avatar asked Oct 19 '12 11:10

AndroidNoob


3 Answers

Here's a simple workaround :

use the following in your onCreate method :

View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);

this collapses the home button completely.

PS : i'm using standard ActionBar but this should work the same

Or

if you want to support in Sherlock Actionbar to than you have to use this

actionBar.setLogo(null); // forgot why this one but it helped

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);

actionBar.setDisplayShowTitleEnabled(false);
like image 50
furyfred Avatar answered Oct 28 '22 21:10

furyfred


It is worth noting that the selected answer will only work with the native ActionBar because the element android.R.id.home doesn't exist on pre-hc. That is, on pre-hc devices, you will get an NPE.

Since you are using ActionBarSherlock, I'm assuming you want to support pre-hc devices so you should use the correct id based on the platform version. ABS will expose the equivalent of android.R.id.home via R.id.abs__home for its implementation of the ActionBar.

So, we can tweak the suggested workaround as:

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);
like image 39
Carlos N Avatar answered Oct 28 '22 22:10

Carlos N


this works great too! (read the bugreport comments ;))

actionBar.setLogo(new ColorDrawable(Color.TRANSPARENT));
like image 28
user764155 Avatar answered Oct 28 '22 22:10

user764155