44

I am having difficulties trying to remove the back/home button from the action bar.

 getActionBar().setDisplayShowHomeEnabled(false);   //disable back button
 getActionBar().setHomeButtonEnabled(false);

In a older android phone, the back button is removed with these two code lines. However with the nexus 4, the back button still appears but is just disabled. Also I am just adding a menu item on the right that behaves like the back/home button replacing the back/home button. What am I missing?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
RyanCW
  • 1,012
  • 2
  • 10
  • 23

10 Answers10

96

Use getActionBar().setDisplayHomeAsUpEnabled(false) to remove the home button from the action bar.

ElectronicGeek
  • 3,312
  • 2
  • 23
  • 35
  • 3
    as you can see I have already tried that and it does not work. – RyanCW Mar 11 '14 at 00:07
  • It's a different method than setDisplayShowHomeEnabled. Did you try the code I posted? The setDisplayShowHomeEnabled method disables or enables the up affordance. The setDisplayHomeAsUpEnabled method puts the home button in the up affordance. – ElectronicGeek Mar 11 '14 at 00:08
  • 1
    thanks buddy sorry about that, obvious fustration here, and I will accept your answer when 5 minutes is up – RyanCW Mar 11 '14 at 00:10
  • Hmm how does this work when you have multiple fragments and only one activity. getActionBar is not accessible from fragment. We should be able to tell the navigation that there is no back button not have to code it. – JPM Oct 29 '21 at 19:46
  • Definitely not working anymore. I have this setting set to `false` all the time, since I implemented a drawer for this button and the button is always visible and usable. – Akito Nov 14 '21 at 01:42
  • It seems that in some kind of situations it only works when you call from `onCreateOptionsMenu(menu: Menu, inflater: MenuInflater)` – marcRDZ May 03 '22 at 11:14
19

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
}

source: https://stackoverflow.com/a/24967862/2887103

Community
  • 1
  • 1
bharv14
  • 493
  • 1
  • 6
  • 15
  • 1
    Answer worked for me. Just that my requirement was to hide, so added the above code without actionBar.setHomeButtonEnabled(false). Since its hidden, there is no need of disabling it. – arungiri_10 Mar 13 '18 at 10:43
12

ElectronicGeeks answer is correct.

For API lower than 11, Use:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Naxos84
  • 1,890
  • 1
  • 22
  • 34
5

To control the up affordance, use setDisplayHomeAsUpEnabled().

David
  • 3,971
  • 1
  • 26
  • 65
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This makes home icon as up but doesn't change it's behavior(opens navigation drawer when clicked). Also after hardware back press it goes back to last screen but the home icon doesn't show up. I am setting setDisplayHomeAsUpEnabled() as false when back button is pressed. – VishalKale Feb 15 '16 at 06:23
4

None of the suggested solutions works for me.

But this one does:

// Hide the back button
mActionBar.setHomeAsUpIndicator(null);

It is a kind of a hack (last resort solution), though, so showing the action bar again means setting its icon back again.

John
  • 185
  • 2
  • 14
  • This is literally the only solution that *actually* worked. Thank you. All other solutions, which are spammed as answers everywhere, do not work! – Akito Nov 14 '21 at 01:48
3

This worked for me :)

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(false);
Sam
  • 241
  • 3
  • 7
2

In the case where you have used toolbar as Action bar:-

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Use the below code to hide the navigation button:-

toolbar.setNavigationIcon(null);
m4n0
  • 29,823
  • 27
  • 76
  • 89
Rishabh
  • 1,827
  • 1
  • 12
  • 9
2

For Kotlin;

(activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnable(false)
Crebain
  • 180
  • 1
  • 8
0

This code work for me

For remove navigation bar getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

For remove status bar getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

ฺBut above code, it show again when you touch on screen, so if you want static state, combine this code.

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE);

Somsak Elect
  • 31
  • 1
  • 6
-1

You can use this code :

toggle.setDrawerIndicatorEnabled(false);

Works great for me.

Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62