android - Showing a menu overflow button in a Toolbar -


i've created toolbar lollipop, can't seem figure out how add overflow button toolbar. i'm not going using v7 appcompat toolbar, want explicitly use toolbar widget. want 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. action bar     // automatically handle clicks on home/up button, long     // specify 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> 

if want add item in overflow menu should set never android:showasaction. if set always item icon in toolbar.

edit menu.xml 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 need call setactionbar(). edit inittoolbar() this:

private void inittoolbar() {     mtoolbar = (toolbar) findviewbyid(r.id.toolbar);     mtoolbar.settitletextcolor(color.white);     mtoolbar.settitle("gnappo");     mtoolbar.showoverflowmenu();     setactionbar(mtoolbar); } 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -