5

How can you disable the default "UP" button in Toolbar beside the ic_launcher? I want to leave only the ic_launcher at the left.

enter image description here

Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52

7 Answers7

8

If you're using Toolbar (android.support.v7.widget.Toolbar or native) in order to disable navigation button just call:

toolbar.setNavigationIcon(null);

From documentation:

 /**
 * Set the icon to use for the toolbar's navigation button.
 * ...
 *  @param icon Drawable to set, may be null to clear the icon
 */
 public void setNavigationIcon(@Nullable Drawable icon) { ... }
zasadnyy
  • 2,107
  • 2
  • 19
  • 26
  • 5
    This is ***hiding*** not ***disabling***. The 2 terms are totally different in this context. Disabling means it's just temporarily unavailable for some reason but will available later. I really don't understand the design of Android, it's too much complicated, even something really simple like this makes we do many hacks. I must say that it's just a bad design. Programmers ***mainly*** need to concentrate on business logic, not all these trifles which should have been prepared/provided easily by the Android core libraries writers. – King King Oct 22 '16 at 03:09
  • Also the most suitable method to use to disable the Home button is `SupportActionBar.SetHomeButtonEnabled`, however it does nothing. – King King Oct 22 '16 at 03:14
1

What Alex K has suggested is for the Back button in navigation bar at the bottom of the screen - not Up icon at the top left corner of the screen.

For the ActionBar up icon you need to add this:

    getActionBar().setDisplayHomeAsUpEnabled(false);

Also, in your manifest.xml file, you could delete the parent metadata if there is any:

        <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.ParentActivity" />
Kasra
  • 3,045
  • 3
  • 17
  • 34
1

For this issue I believe it is because you are using the navigationIcon as the attribute to set the icon on the toolbar.

<item name="navigationIcon">@drawable/tool_bar_title_logo</item>

When using navigationIcon as an attribute to set the toolbar icon:

supportActionBar?.setDisplayHomeAsUpEnabled(false) // won't work

It will not be able to turn off the onClick for the icon.


Instead, use setIcon(R.drawable.tool_bar_title_icon) to set icon for the toolbar:

supportActionBar?.setIcon(R.drawable.tool_bar_title_logo) // to set the toolbar icon

Then disable it using:

supportActionBar?.setDisplayHomeAsUpEnabled(false) 
Haomin
  • 1,265
  • 13
  • 12
0

Insert this code to the Activity where you want to hide UP button from Toolbar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // remove UP button
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    }
}
David Vávra
  • 18,446
  • 7
  • 48
  • 56
0

I meet the similar question, the empty navigationIcon holder the left space.

use app:contentInsetLeft="0dp" in the xml of <android.support.v7.widget.Toolbar> , it fixs

Ninja
  • 2,479
  • 3
  • 23
  • 32
0

There are two ways in the manifest which cause the Up navigation to appear in the AppBar. As mentioned by @Kasira:

<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.ParentActivity" />

And also if the <activity> tag contains: android:parentActivityName=".activity.ParentActivtyName"

Crompy
  • 645
  • 1
  • 5
  • 11
0

I did a lot of searching online but I could not find any solution to this problem. So, I poked around in the source code a little bit and came to this solution:

val field = Class.forName("androidx.appcompat.widget.Toolbar").getDeclaredField("mNavButtonView")
field.isAccessible = true
val toolbarUpButton = field.get(findViewById(R.id.main_toolbar)) as? ImageButton
toolbarUpButton?.isEnabled = false

This does not hide the up button but disables it and if you want to indicate that the button is disabled you can use the following selector as the color for your drawable:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/enabled" android:state_enabled="true"/>
    <item android:color="@color/disabled" android:state_enabled="false"/>
    <item android:color="@color/enabled" />
</selector>
LeFrosch
  • 111
  • 1
  • 2
  • 11