9

I would like to do an edit mode, in the style of the tablet gmail app. If the user presses the edit button on the actionbar, than I would like to show him/her an action view, that has a done button on the left side, and a delete button on the right.

I have an example that works without actionbarsherlock here: https://code.google.com/p/romannurik-code/source/browse/misc/donediscard

I would like to stick to actionbarsherlock, for compatibility reasons.

This is the way I solved it in onCreateOptionsMenu:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
getSupportActionBar().setIcon(R.drawable.white);

for (int i = 0; i < menu.size(); i++) {
    menu.getItem(i).setVisible(false); }

getSupportActionBar().setDisplayHomeAsUpEnabled(false); does nothing, so I had to set the home icon to a white 1x1 pixel drawable.

I also had to set the background color of the actionbar, as the actionview's background color. If I had not, than the 1x1 home icon would have padding around it, and the original background color would be visible around the white home button.

Anyone got a better solution for this?

edit: I also had to change the style:

<style name="Theme.Styled" parent="Theme.Sherlock.Light">
        <item name="android:homeAsUpIndicator">@drawable/white</item>
        <item name="homeAsUpIndicator">@drawable/white</item>
</style>

Also..settings android:homeAsUpIndicator increased my min api level from 8 to 11 which is also an issue.

berestom
  • 1,109
  • 2
  • 10
  • 9

4 Answers4

36

If you're on API level 14 or above and are not using ActionbarSherlock, this code in onCreateOptionsMenu will disable the up button, remove the left caret, and remove the icon:

ActionBar actionBar = getActionBar();
if (actionBar != null) {
    actionBar.setHomeButtonEnabled(false); // disable the button
    actionBar.setDisplayHomeAsUpEnabled(false); // remove the left caret
    actionBar.setDisplayShowHomeEnabled(false); // remove the icon
}
VinceFior
  • 1,279
  • 13
  • 25
  • I used your code and its working fine. I am able to apply my logo as home button, but i want to set text also with back button. look like image in link : http://stackoverflow.com/questions/29486908/android-action-bar-home-button-customize . Can you help me out in this? – Manoj Fegde Apr 07 '15 at 09:57
  • 2
    This worked for me! I just had to change ```getActionBar()``` to ```getSupportActionBar()```. Thanks! – julianwyz Jan 08 '16 at 14:26
5

You were almost there. To hide the icon/logo completely, use setDisplayShowHomeEnabled(false). The call you're using just removes the little arrow that indicates that the icon acts as an "up" button also.

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
Geobits
  • 22,218
  • 6
  • 59
  • 103
  • setDisplayShowHomeEnabled(false) does nothing. Oh and now I'm stuck with that stupid little arrow. How the heck do I make it disappear. The code I pasted in the opening post does not work anymore for me \ : – berestom Feb 26 '13 at 12:44
  • Oh I got it..updated opening post. I accidently deleted some lines from styles.xml. – berestom Feb 26 '13 at 14:19
  • I use my drawable as getActionBar().setLogo(R.drawable.back_button); after getActionBar().setDisplayHomeAsUpEnabled(false);. I want to write text with that. Like in image as: http://stackoverflow.com/questions/29486908/android-action-bar-home-button-customize – Manoj Fegde Apr 07 '15 at 09:59
0

This worked for me, though its a slight change in the above mentioned solution

android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(false); // disable the button
            actionBar.setDisplayHomeAsUpEnabled(false); // remove the left caret
            actionBar.setDisplayShowHomeEnabled(false); // remove the icon
        }
Akhila
  • 3,235
  • 1
  • 14
  • 30
-1

Though it's an older post, I would like to share an answer which worked for me.

To hide the UP button in actionbar, use the following in OnCreateOptionsMenu:

if (getSupportActionBar() != 
           getSupportActionBar().hide();
       }

To remove the UP button in actionbar, use the following in OnCreateOptionsMenu:

if (getSupportActionBar() != 
           getSupportActionBar().setDisplayHomeAsUpEnabled(false);
       }

I hope it will help newbies.

David Harris
  • 2,332
  • 1
  • 13
  • 25
justLearning
  • 299
  • 2
  • 7