27

The following code causes a back arrow to appear in the ActionBar:

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

I'm looking for the resource ID of the arrow drawable, i.e. android.R.drawable.xxx. The reason I need this ID is so that I can manually set an identical arrow (size & colour) elsewhere in my app.

I tried making my own drawable and using that but the size was different from the one in the ActionBar.

hmuss
  • 301
  • 1
  • 3
  • 5

4 Answers4

56

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.

VorteXavier
  • 1,160
  • 1
  • 11
  • 17
17

If you need to use the default back arrow of Android SDK and also with AndroidX then you can reach it with the following

androidx.appcompat.R.drawable.abc_ic_ab_back_material

Additionally, if you are planning to use it for a custom toolbar then you can set it like this

toolbar.setNavigationIcon(androidx.appcompat.R.drawable.abc_ic_ab_back_material)

And if you need to set click listener

toolbar.setNavigationOnClickListener { doSomething() }
Eren Utku
  • 1,731
  • 1
  • 18
  • 27
3

You can easily create a back arrow using Android asset studio.

Click on res folder and then right click on drawable -> New -> Vector Asset

enter image description here

enadun
  • 3,107
  • 3
  • 31
  • 34
2

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);
}
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74