Nxt3 Nxt3 - 3 months ago 23
Android Question

Showing a menu overflow button in a Toolbar

I'm just messing around with Android and trying to learn stuff by building on a dummy app. 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>

Answer

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);
}
Comments