android - setOnItemClickListener Not launching Activity in Navigation Drawer -
i following tutorial implement navigation drawer. http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
i able set title in actionbar not launching activity.
i have reffered to:- setonitemclicklistener() in navigation drawer not working? , navigation drawer setonitemclicklistener not working , many other links.
this xml navigation drawer , activity_main
.
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <framelayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- listview display slider menu --> <listview android:id="@+id/list_slidermenu" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@color/list_divider" android:dividerheight="1dp" android:listselector="@drawable/list_selector" android:background="@color/list_background"/> <android.support.design.widget.coordinatorlayout android:id="@+id/coordinatorlayout" 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=".mainactivity"> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:fbutton="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_height="wrap_content" android:paddingleft="16dp" android:paddingright="16dp" android:paddingtop="16dp" android:paddingbottom="16dp" tools:context=".mainactivity"> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ll1"> <spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinner1" android:layout_weight="0.5" android:spinnermode="dropdown" /> <spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinner2" android:layout_weight="0.5" /> </linearlayout> <linearlayout android:layout_below="@+id/ll1" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ll2"> <edittext android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputtype="number" android:ems="10" android:ellipsize="start" android:gravity="center_horizontal" android:id="@+id/amount" android:hint="enter budget" /> </linearlayout> <relativelayout android:id="@+id/llr3" android:layout_below="@+id/ll2" android:layout_width="match_parent" android:layout_height="wrap_content"> <linearlayout android:weightsum="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ll3"> <info.hoang8f.widget.fbutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.5" android:layout_marginright="2dp" android:text="more talktime less validity" android:id="@+id/calculate" fbutton:buttoncolor="@color/fbutton_color_peter_river" fbutton:shadowcolor="@color/fbutton_color_belize_hole" fbutton:shadowenabled="true" fbutton:shadowheight="5dp" fbutton:cornerradius="5dp" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_alignparentend="true" /> <info.hoang8f.widget.fbutton android:layout_marginleft="2dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="more validity less talktime" android:id="@+id/calculate2" fbutton:buttoncolor="@color/fbutton_color_pumpkin" fbutton:shadowcolor="@color/fbutton_color_pomegranate" fbutton:shadowenabled="true" fbutton:shadowheight="5dp" fbutton:cornerradius="5dp" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_toleftof="@+id/calculate" android:layout_tostartof="@+id/calculate" /> </linearlayout> </relativelayout> <android.support.v7.widget.cardview android:id="@+id/card_view" android:layout_gravity="center" android:layout_width="fill_parent" android:layout_height="wrap_content" card_view:cardelevation="10dp" card_view:cardcornerradius="4dp" card_view:contentpadding="10dp" android:layout_marginleft="5dp" android:layout_margintop="5dp" android:layout_marginright="5dp" android:layout_above="@+id/connect" android:layout_below="@+id/llr3"> <listview android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/res" android:layout_below="@+id/llr3" /> <com.top.up.planner.mytextviewnormal android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_below="@+id/listview" android:id="@+id/res" android:layout_centerhorizontal="true" android:layout_alignparentbottom="true"/> </android.support.v7.widget.cardview> <com.top.up.planner.mytextviewnormal android:layout_width="fill_parent" android:textsize="18dp" android:layout_height="wrap_content" android:gravity="center" android:id="@+id/connect" android:layout_centerhorizontal="true" android:layout_alignparentbottom="true"/> </relativelayout> </android.support.design.widget.coordinatorlayout> </android.support.v4.widget.drawerlayout>
my java code:-
private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; // nav drawer title private charsequence mdrawertitle; // used store app title private charsequence mtitle; // slide menu items private string[] navmenutitles; private typedarray navmenuicons; private arraylist<navdraweritem> navdraweritems; private navdrawerlistadapter navadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtitle = mdrawertitle = gettitle(); // load slide menu items navmenutitles = getresources().getstringarray(r.array.nav_drawer_items); // nav drawer icons resources navmenuicons = getresources() .obtaintypedarray(r.array.nav_drawer_icons); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.list_slidermenu); navdraweritems = new arraylist<navdraweritem>(); // adding nav drawer items array // home navdraweritems.add(new navdraweritem(navmenutitles[0], navmenuicons.getresourceid(0, -1))); // find people navdraweritems.add(new navdraweritem(navmenutitles[1], navmenuicons.getresourceid(1, -1))); // recycle typed array navmenuicons.recycle(); mdrawerlist.bringtofront(); mdrawerlayout.requestlayout(); mdrawerlist.setonitemclicklistener(new slidemenuclicklistener()); navadapter = new navdrawerlistadapter(getapplicationcontext(), navdraweritems); mdrawerlist.setadapter(navadapter); // enabling action bar app icon , behaving toggle button getsupportactionbar().setdisplayhomeasupenabled(true); getsupportactionbar().sethomebuttonenabled(true); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.mipmap.ic_drawer, //nav menu toggle icon r.string.app_name, // nav drawer open - description accessibility r.string.app_name // nav drawer close - description accessibility ) { public void ondrawerclosed(view view) { getsupportactionbar().settitle(mtitle); // calling onprepareoptionsmenu() show action bar icons invalidateoptionsmenu(); } public void ondraweropened(view drawerview) { getsupportactionbar().settitle(mdrawertitle); // calling onprepareoptionsmenu() hide action bar icons invalidateoptionsmenu(); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { // on first time display view first nav item displayview(0); } public class slidemenuclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // display view selected nav drawer item displayview(position); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { super.onoptionsitemselected(item); if (mdrawertoggle.onoptionsitemselected(item)) { return true; } switch(item.getitemid()) { case r.id.item1: onshare(); return true; case r.id.item2: intent intent = new intent(getapplicationcontext(),help.class); startactivity(intent); return true; default: return super.onoptionsitemselected(item); } } @override public boolean onprepareoptionsmenu(menu menu) { // if nav drawer opened, hide action items boolean draweropen = mdrawerlayout.isdraweropen(mdrawerlist); //menu.finditem(r.id.action_settings).setvisible(!draweropen); return super.onprepareoptionsmenu(menu); } private void displayview(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = new homefragment(); break; case 1: fragment = new findpeoplefragment(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.frame_container, fragment).commit(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); mdrawerlist.setselection(position); settitle(navmenutitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } else { // error in creating fragment log.e("mainactivity", "error in creating fragment"); } } @override public void settitle(charsequence title) { mtitle = title; getsupportactionbar().settitle(mtitle); } /** * when using actionbardrawertoggle, must call during * onpostcreate() , onconfigurationchanged()... */ @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(android.content.res.configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggls mdrawertoggle.onconfigurationchanged(newconfig); }
and drawble being set arrow, have coded show hamburger icon how fix ?
try adding oncreate : mdrawertoggle.syncstate();
Comments
Post a Comment