Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How provide Up navigation with toolbar's home button on v7 toolbar

I have a toolbar in my activity ( import android.support.v7.widget.Toolbar; ) and I'm trying to provide Up navigation using its home button. What I have:

Manifest:

<!-- ... -->
<activity android:name=".SettingsActivity"
          android:label="@string/settings"
          android:parentActivityName=".MainActivity"/>
<!-- ... -->

view_toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp">
</android.support.v7.widget.Toolbar>

activity_settings.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Toolbar -->
    <include
        layout="@layout/view_toolbar" />

    <!-- ... -->

my onCreate method:

super.onCreate(bundle)
setContentView(R.layout.activity_settings);

// Set the toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

So far I shouldn't have an up button and I don't. So we're fine. But when I tried to add it, I couldn't.

First I tried this:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Didn't work. Then I tried this (as shown here):

toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    Toast.makeText(ToolbarActivity.this, "Up clicked", 
        Toast.LENGTH_SHORT).show();
    NavUtils.navigateUpFromSameTask(ToolbarActivity.this);
}
});

I even tried a workaround I saw somewhere, involving creating a dummy menu and trying to get the event from onOptionsItemSelected (which is never called by the way).

What can I do? What is the correct way to provide Up navigation through toolbar?

like image 519
TheCrafter Avatar asked Dec 15 '15 00:12

TheCrafter


2 Answers

Did you have the below code to set your toolbar as the default activity actionbar?

setSupportActionBar(toolbar);

You did not set an image icon to the home button, maybe it shows up, but you just can't see it.

getSupportActionBar().setHomeAsUpIndicator(iconDrawable);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and as @Gonzalo said, you also have to override the menu select event to handle the home button onClick event

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        //handle the home button onClick event here.
        return true;
    case android.R.id.other_menu
        return true
    }

    return super.onOptionsItemSelected(item);
}

and why didn't you have a appbarlayout to contain the toolbar?

<android.support.design.widget.AppBarLayout
        android:id="@+id/baseAppbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/baseToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

More actionbar implement details please have a look at my github project, hope it can help:

java code https://github.com/DanielShum/MaterialAppBase/blob/master/materialAppBaseLibrary/src/main/java/com/daililol/material/appbase/base/BaseActionbarActivity.java

xml code https://github.com/DanielShum/MaterialAppBase/blob/master/materialAppBaseLibrary/src/main/res/layout/base_actionbar_activity.xml

like image 102
Daniel Martin Shum Avatar answered Sep 28 '22 09:09

Daniel Martin Shum


1- set your toolbar

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

2- setup your icon

    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

    }

3- override this method

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}
like image 24
Bishoy Kamel Avatar answered Sep 28 '22 10:09

Bishoy Kamel