I'm migrating from ActionBar
to Toolbar
in my application.
But I don't know how to display and set click event on Back Arrow on Toolbar
like I did on Actionbar
.
With ActionBar
, I call mActionbar.setDisplayHomeAsUpEnabled(true)
.
But there is no the similar method like this.
Has anyone ever faced this situation and somehow found a way to solve it?
In your manifest file for the activity where you want to add a back button, we will use the property android:parentActivityName
P.S. This attribute was introduced in API Level 16.
If you were using
AppCompatActivity
and have gone down the path of not using it, because you wanted to not get the automaticActionBar
that it provides, because you want to separate out theToolbar
, because of your Material Design needs andCoordinatorLayout
orAppBarLayout
, then, consider this:You can still use the
AppCompatActivity
, you don't need to stop using it just so that you can use a<android.support.v7.widget.Toolbar>
in your xml. Just turn off the action bar style as follows:First, derive a style from one of the NoActionBar themes that you like in your
styles.xml
, I usedTheme.AppCompat.Light.NoActionBar
like so:In your App's manifest, choose the child style theme you just defined, like so:
In your Activity Xml, if the toolbar is defined like so:
Then, and this is the important part, you set the support Action bar to the AppCompatActivity that you're extending, so that the toolbar in your xml, becomes the action bar. I feel that this is a better way, because you can simply do the many things that ActionBar allows, like menus, automatic activity title, item selection handling, etc. without resorting to adding custom click handlers, etc.
In your Activity's onCreate override, do the following:
Add this to activity's xml in layout folder:
Make toolbar clickable, add these to onCreate method:
you can use the tool bar setNavigationIcon method. Android Doc