How can I set back arrow in Android toolbar and also apply click listener?
 
    
    - 5,031
- 17
- 33
- 41
 
    
    - 1,740
- 2
- 16
- 21
- 
                    make custom action bar...check [this](http://stackoverflow.com/a/33800352/2553431) – Iamat8 Mar 05 '16 at 05:03
- 
                    Related post - [Display Back Arrow on Toolbar](https://stackoverflow.com/q/26651602/465053) – RBT Aug 08 '18 at 11:28
7 Answers
First make one toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
then include it in activity_main.xml like this way:
<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
</LinearLayout>
then in your MainActivity.java file, put this code:
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("MyTitle");
To add listener on back press, use following method:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // todo: goto back activity from here
            Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
    
    - 1,524
- 20
- 31
 
    
    - 2,213
- 1
- 17
- 29
Toolbar mToolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);    
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // perform whatever you want on back arrow click 
    }
});
// with lamda expression
toolbar.setNavigationOnClickListener(view -> onBackPressed());
 
    
    - 4,437
- 42
- 39
 
    
    - 1,860
- 17
- 17
- 
                    2In my case, you must add the listener after setSupportActionBar or it won't work. – Jiawei Yang Jul 18 '18 at 02:32
- 
                    What @JiaweiYang is important, it'd be good if you add to your answer...(+1) – Leo Jan 07 '19 at 11:01
- 
                    the lamda version doesn't work i get error `Lamda expression are not supported at language leve 7` – Ahtisham Apr 11 '20 at 05:55
If you are using the default back button for android by using
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Then override the onOptionsItemSelected like
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            //do whatever
            return true;
        default:
           return super.onOptionsItemSelected(item);
    }
}
 
    
    - 2,173
- 1
- 19
- 26
Complete example here http://www.freakyjolly.com/how-to-add-back-arrow-in-android-activity/
Use getSupportActionBar() Activity on which you want to show Back Icon
In OtherActivity.class
public class OtherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.other_activity);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}
public boolean onCreateOptionsMenu(Menu menu) {
    return true;
}
}
This will add a event listen
 
    
    - 9,626
- 4
- 66
- 46
If you want to know when home is clicked is an AppCompatActivity then you should try it like this: Use this code :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Listen for click events on android.R.id.home like usual:
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    if (menuItem.getItemId() == android.R.id.home) {
         Intent intent = new Intent(CurrentActivity.this, MainActivity.class);
         startActivity(intent);
         finish();
    }
    return super.onOptionsItemSelected(menuItem);
}
 
    
    - 16,571
- 12
- 101
- 98
Add this
 Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
 setSupportActionBar(toolbar);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 getSupportActionBar().setDisplayShowHomeEnabled(true);
and in onOptionsItemSelected add this
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Check this
Display Back Arrow on Toolbar Android
http://developer.android.com/intl/es/training/implementing-navigation/ancestral.html
Very simple code. Add inside onCreateView() method of activity
To display icon
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and to add click listener
 toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // Put your click logic here
            }
        });
 
    
    - 341
- 2
- 12

 
    