The following code causes a back arrow to appear in the ActionBar:
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsU
The id of the back button in Toolbar is
android.R.id.home
You can take action from onOptionsItemSelected Method on Activity.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
//Do your task here.
return true;
}
return super.onOptionsItemSelected(item);
}
You can easily create a back arrow using Android asset studio.
Click on res
folder and then right click on drawable -> New -> Vector Asset
If you have the support library in your project, you can make a back button in any place in your applicaction like this:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="?attr/homeAsUpIndicator"
android:background="?attr/selectableItemBackgroundBorderless"/>
Specifically the resource for the back arrow is ?attr/homeAsUpIndicator
.