6

I've created a Toolbar for Lollipop, but I can't seem to figure out how to add an overflow button to the toolbar. I'm not going to be using the v7 appcompat toolbar, as I want to explicitly use the Toolbar widget. I want it to look something like this:

enter image description here

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="112dp"
    android:elevation="4dp"
    android:layout_alignParentTop="true"
    android:minHeight="?android:attr/actionBarSize"
    android:background="?android:attr/colorPrimary"
    android:gravity="bottom"
    android:paddingBottom="16dp"/>
</RelativeLayout>

Main class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quotebook);


    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    getWindow().setStatusBarColor(getResources().getColor(R.color.primary_dark));
    getWindow().setNavigationBarColor(getResources().getColor(R.color.primary_dark));

    initToolbar();

}

private void initToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mToolbar.setTitleTextColor(Color.WHITE);
    mToolbar.setTitle(R.string.app_shortname);
    mToolbar.showOverflowMenu();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_overflow, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    android:showAsAction="always"
    android:visible="true"/>

</menu>
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Nxt3
  • 1,970
  • 4
  • 30
  • 52

2 Answers2

10

If you want to add the item in the overflow menu you should set never as android:showAsAction. If you set always the item will be an icon in the toolbar.

Edit your menu.xml like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        android:showAsAction="never"
        android:visible="true" />

</menu>

Also you need to call setActionBar(). Edit initToolbar() like this:

private void initToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mToolbar.setTitleTextColor(Color.WHITE);
    mToolbar.setTitle("gnappo");
    mToolbar.showOverflowMenu();
    setActionBar(mToolbar);
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
  • I'm not trying to add an item to the menu--I'm trying to add the actual menu button. I can't get the overflow menu to show up at all, and I'm not sure how to. – Nxt3 May 11 '15 at 17:13
  • You can't have the overflow menu without item in it. If you set `android:show as action="never"` the overflow shows up with the item _settings_ – Mattia Maestrini May 11 '15 at 17:17
  • That's what I'm saying. I'm using what you gave me. I don't even have the overflow menu icon appearing in my toolbar. – Nxt3 May 11 '15 at 17:21
  • If we have to set all the things via support action bar then what is the question of using toolbar? – blackHawk Aug 05 '17 at 19:14
3

If your toolbar is of the package android.support.v7.widget, use setSupportActionBar(mToolbar); instead of setActionBar(mToolbar);

fat
  • 6,435
  • 5
  • 44
  • 70
sudesh
  • 51
  • 4