2

I need to have an arrow "back" button on the left side of the toolbar, and for some reason it points right instead of left.

How can I set it to the opposite direction?

This is my code:

mToolbar = (Toolbar) findViewById(R.id.machine_tool_bar);        
setSupportActionBar(mToolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

EDIT: my device language is right-to-left. I tries to set layoutDirection LTR but it didn't help..

Maayan Castel
  • 133
  • 1
  • 13

1 Answers1

6

Maayan,

The reason that the back button is pointing to the right is that in RTL locales the drawable is taken from the drawable-ldrtl folder, found in the support library.

The approach I found most useful is overriding the drawable of the arrow button.

First, you need to take the arrow that is pointing left. It can be easily obtained from Google's Material design icons: Material Design Icons - Back Arrow

Now, there are two ways that you can handle this:

  1. Use mToolbar.setNavigationIcon(int resId) as follows:
mToolbar = (Toolbar) findViewById(R.id.machine_tool_bar);  
mToolbar.setNavigationIcon(R.drawable.left_pointing_arrow);
  1. If you don't want to use setNavigationIcon in the code, you can simply override the resource in the drawable folders. The back arrow is found in the support library AppCompat-v7: /extras/android/support/appcompat-v7/appcompat-v7-{version}

    The resource name of the drawable you are looking for is: abc_ic_ab_back_mtrl_am_alpha

    You can simply copy the corresponding libraries from the support library to your resources folder, and override the assets with the drawable that you downloaded.

    enter image description here

    Note that this works only from API-17 (where RTL support was introduced), as the folders contain the 'ldrtl' attribute.

Hope it helps.

guychu
  • 111
  • 4