16

I tried everything mentioned here on all Stackoverflow's other answers but its not working out. Here is my code.

actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.actionbar_layout);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setLogo(null);
actionBar.setDisplayShowTitleEnabled(false);
View homeIcon = findViewById(android.R.id.home);
((View) homeIcon.getParent()).setVisibility(View.GONE);
Mihir
  • 2,064
  • 2
  • 21
  • 28

4 Answers4

38

The tabs show on top when you hide the Home item. It is a bit counter intuitive but it also makes some design sense. They're essentially nudging you to use the tabs as titles for the sections and use the action bar below them for actions inside those sections.

You need setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME) to bring the tabs down again. It will probably need a non-null logo too (you can make a 1px transparent one in -nodpi to make it disappear)

Delyan
  • 8,881
  • 4
  • 37
  • 42
  • 3
    I've tried a number of solutions, this is the first one that works! – abc32112 Dec 01 '13 at 09:01
  • 3
    I've one problem .. I can't hide the app icon which displays beside my custom view of my actionBar – mnagy Jan 18 '14 at 17:12
  • you can also just use a stransparent color as icon: @android:color/transparent @android:color/transparent – martyglaubitz Jun 07 '14 at 09:52
  • @Delyan there is a transparent free space at the left of action bar after doing what you have mentioned above. I am using a custom icon using xml so this creates an extra space from left which i don't want. Any idea how to completely remove that space ? – Abid Khan Aug 04 '14 at 10:28
  • Btw, there is a [bug report](http://code.google.com/p/android/issues/detail?id=36191) for this. – Vadim Kotov Nov 04 '14 at 11:26
4

Using the accepted solution leaves a left padding on the actionbar, which shrinks your custom view.

the solution is to add this code in the onCreate of the Activity:

        View homeIcon = findViewById(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.id.home : R.id.abs__home);
        ((View) homeIcon.getParent()).setLayoutParams(new LinearLayout.LayoutParams(0, 0));
        ((View) homeIcon).setVisibility(View.GONE);
marmor
  • 27,641
  • 11
  • 107
  • 150
2
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME); 
        getActionBar().setDisplayUseLogoEnabled(true);
        getActionBar().setDisplayShowCustomEnabled(true);
        getActionBar().setCustomView(R.layout.actionbar_layout);

This worked for me.

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Ajay
  • 111
  • 2
1

Please try to use LayoutInflater to inflate the view then set the view to the actionbar.

    LayoutInflater inflator = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflator.inflate(R.layout.layout, null);
    actionBar.setCustomView(v);

And from this

use RelativeLayout instead of LinearLayout as the main container. It's important to have android:layout_gravity="fill_horizontal" set for it. That should do it.

Community
  • 1
  • 1
Ye Lin Aung
  • 11,234
  • 8
  • 45
  • 51