Search code examples
androidandroid-actionbarandroid-homebuttonappbar

Is it possible remove home button from actionbar


On my main activity, where there is no way to go back, I'd like to remove the app "home" button in the actionbar.

I think it is confusing for the user, who still can quit the app with the OS back button.

Browsing stackoverflow, I saw a whole lot of people asking this, and not a single answer worked for me. Here is the list :

The answers were usually redondant and could be summarized to following :

ActionBar supportActionBar = getSupportActionBar(); //I target some low API
if(supportActionBar != null) {
    supportActionBar.setDisplayShowHomeEnabled(false);
    supportActionBar.setDisplayHomeAsUpEnabled(false);
    supportActionBar.setHomeButtonEnabled(false);
    supportActionBar.setHomeAsUpIndicator(null);
}

I tried every combination of those and nothing worked, whether I try with the default ActionBar or with an XML-declared compat-v7 ToolBar with setSupportActionBar().

I also couldn't see any question adapted for the now recommanded App Bar, and an answer with it would be even greater.

Here is my activity manifest :

<activity
    android:name=".activity.WelcomeActivity"
    android:logo="@drawable/ic_launcher"
    android:label="@string/long_app_name"
    android:launchMode="singleTop"/>

So, is it even possible to remove this annoying useless arrow nowadays ?


Solution

  • Create a new style in styles.xml:

     <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>
    

    Set this style to your activity in Manifest file. To add the Toolbar, add this to your layout file:

    <android.support.v7.widget.Toolbar
            android:background="@color/colorPrimary"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.Toolbar>
    

    and then in your Java class:

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

    This will remove the top left arrow.