How add a single menu item left to the toolbar in android?

27.6k views Asked by At

I simply want a back button in left side of toolbar.But when i added with following code ,appear in right side of toolbar.how can i change to it to left side?

my code

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context="com.me.myapp.activities.Timer">
    <item
        android:id="@+id/backButton"
        android:title="Back Button"
        android:icon="@mipmap/back_icon"
        app:showAsAction="ifRoom"></item>
</menu>
1

There are 1 answers

1
Vikalp Patel On BEST ANSWER

You just need Back icon on top left side of ToolBar then just configure Toolbar.

mToolBar = (Toolbar) findViewById(R.id.toolbarLayout);
mToolBar.setTitle("Toolbar");
mToolBar.setNavigationIcon(R.drawable.ic_back_shadow);
setSupportActionBar(mToolBar);

As ToolBar menu items are totally depend on either your device is on RTL(Right-To-Left) support or not which are mainly used for menu items and not for back key.

Moreover you can handle that back icon with

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case android.R.id.home:
        finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}