Toolbar - add the up button

Guilherme picture Guilherme · Dec 17, 2014 · Viewed 15.2k times · Source

I am trying to use the Toolbar instead of the ActionBar, but I can't figure out how to add the up button to return to the previous activity. I couldn't find any method that could relate to it.

How do I add the up button?

Answer

Jan Omacka picture Jan Omacka · Dec 17, 2014

I guess what you are looking for is something like this:

Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar_detail);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Or in case of using in Fragment:

Toolbar toolbar = (Toolbar) view.findViewById(R.id.app_bar_detail);
((ActionBarActivity) getActivity()).setSupportActionBar(toolbar);
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

This will show up your Action Bar inside of your toolbar, but don't worry everything will fit together well. The last you have to do if you dont want any shadow under your action bar or any background of it is change your theme in vaules/styles.xml.

<style name="AppThmeme.Base" parent="Theme.AppCompat.NoActionBar">