Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 246
Lejin KR Avatar asked Jun 17 '15 08:06

Lejin KR


1 Answers

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);
    }
}
like image 147
Vikalp Patel Avatar answered Oct 17 '22 07:10

Vikalp Patel