How to create a back button in material design

I cannot find a tutorial on adding this button in the action bar in Material Design.

example image

How can I add this to the action bar on Lollipop?

+7
android button android-actionbar
source share
5 answers

Material Tutorial This will give you a brief idea of ​​how to implement a tangible application.

If you are using ActionBarActivity with the AppCompat Theme , use:

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

You may also need to call setHomeButtonEnabled(true) in the same way. It will look like this:

enter image description here

+12
source share

try it

in on create:

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

in your activity class (assuming you want to close this activity)

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; default: return super.onOptionsItemSelected(item); } } 
+17
source share

in your onCreate add these lines

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); 

for reverse navigation you need to determine the reverse navigation activity on AndroidMnifest.xml

 <activity android:name=".CurrentActivity" android:label="@string/app_name" android:parentActivityName=".BackActivity"> </activity> 
+3
source share

getSupportActionBar().setDisplayHomeAsUpEnabled(true); can throw a nullpointer exception, onCreate() should be like this.

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setupActionBar(); } /** * Set up the {@link android.app.ActionBar}, if the API is available. */ private void setupActionBar() { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { // Show the Up button in the action bar. actionBar.setDisplayHomeAsUpEnabled(true); } } 
+2
source share


First , you must use Theme for Material Design , and Theme supports an ActionBar such as Theme.AppCompat.Light , Theme.AppCompat.Light.DarkActionBar .
Second , call ActionBar.setDisplayHomeAsUpEnabled(true); or ToolBar.setDisplayHomeAsUpEnabled(true); then the Return icon will appear.

+1
source share

All Articles