android - Adroid menu with ActionBarDrawerToggle -
i have simple task:
create menu few items opens on toggle action bar button.
my activity_main is:
<android.support.v4.widget.drawerlayout 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" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.mt.gmtelandroid.mainactivity" android:id="@+id/drawer_layout"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent"> </linearlayout> <android.support.design.widget.navigationview android:layout_width="wrap_content" android:layout_height="match_parent" app:menu="@menu/navigation_menu" android:layout_gravity="start" app:headerlayout="@layout/navigation_header" > </android.support.design.widget.navigationview> and navigation menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/meni1" > <item android:id="@+id/navhome" android:title="home" android:icon="@mipmap/ic_home_black_24dp" > </item> <item android:id="@+id/navtasks" android:title="zadaci" android:icon="@mipmap/ic_drive_eta_black_24dp" ></item> <item android:id="@+id/navlogout" android:title="odjava" android:icon="@mipmap/ic_close_black_24dp"> </item> <item android:id="@+id/myname" android:title="moj nalog" android:icon="@mipmap/ic_account_circle_black_24dp"> </item> main activity is
public class mainactivity extends appcompatactivity { private drawerlayout mdrawerlayout; private actionbardrawertoggle mtogle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mdrawerlayout = (drawerlayout)findviewbyid(r.id.drawer_layout); mtogle = new actionbardrawertoggle(this, mdrawerlayout,r.string.open_menu, r.string.close_menu); mdrawerlayout.adddrawerlistener(mtogle); mtogle.syncstate(); getsupportactionbar().setdisplayhomeasupenabled(true); } @override public boolean onoptionsitemselected(menuitem item) { if (mtogle.onoptionsitemselected(item)) { return true; } return super.onoptionsitemselected(item); } } i don't know how add listener onmenuitemclick can handle happens when user clicks on menu item. have tried adding code onoptionsitemselected method, when debuging found out method called on toggle buton click, not on menu item click!?
you have implement callbacks navigationview.onnavigationitemselectedlistener in activity below,
public class homeactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener than handle click event of navigationview below code
@override public boolean onnavigationitemselected(menuitem menuitem) { // handle navigation view item clicks here. int id = item.getitemid(); intent i; if (id == r.id.navhome) { // perform action here } else if (id == r.id.navtasks) { // perform action here } else if (id == r.id.navlogout) { // perform action here }else if (id == r.id.myname) { // perform action here } drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); drawer.closedrawer(gravitycompat.start); return true; }
Comments
Post a Comment