Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toolbar - add the up button

Tags:

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?

like image 899
Guilherme Avatar asked Dec 17 '14 20:12

Guilherme


People also ask

What is up button in android Studio?

Your app should make it easy for users to find their way back to the app's main screen. One simple way to do this is to provide an Up button on the app bar for all activities except the main one. When the user selects the Up button, the app navigates to the parent activity.


2 Answers

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"> 
like image 50
Jan Omacka Avatar answered Oct 23 '22 16:10

Jan Omacka


If you want to do this in XML, you can use...

<android.support.v7.widget.Toolbar             app:navigationIcon="?homeAsUpIndicator"             ... 
like image 27
gsysko Avatar answered Oct 23 '22 17:10

gsysko