android - Connecting toolbar with the navigation drawer -
i have several activities want use same navigation drawer. here's part of activity class code related drawer:
toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayshowtitleenabled(false); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.adddrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this);
i'm planning make draweractivity class , extend activities want navigation drawer. therefore, have move drawer code draweractivity.
the problem:
toolbar
important initialization of actionbardrawertoggle
. every activity has different toolbar depending upon needs of activity. therefore, toolbar layouts must kept in respective layout xml files. how use toolbar in draweractivity class?
layout xml drawer:
<?xml version="1.0" encoding="utf-8"?> <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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:opendrawer="start"> <framelayout android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/frame_layout" xmlns:android="http://schemas.android.com/apk/res/android"> </framelayout> <android.support.design.widget.navigationview android:id="@+id/nav_view" app:itembackground="@drawable/drawer_list_selector" style="@style/navigationdrawer" app:headerlayout="@layout/sidenav_header" app:menu="@menu/side_navigation_menu" app:theme="@style/drawer"/> </android.support.v4.widget.drawerlayout>
here's sidenavigation activity (which i'm trying extend classes need navigation drawer): pastebin.com/hhemxku5
main statement of log says:
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.support.v7.app.actionbar.setdisplayshowtitleenabled(boolean)' on null object reference
here's link oncreate of 1 of activity class extends sidenavigation class: pastebin.com/ikyxvbda
okey want use drawer activity. posting code here. hope out.
first of need develop navigationdrawer
. know can self.
now create child activity example home.java , extend drawer activity let's mainactivity.java
.
it this.
public class home extends mainactivity{ toolbar toolbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } }
now perform setcontentview
in oncreate
, bur here this.
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getlayoutinflater().inflate(r.layout.home, framelayout); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); getsupportactionbar().sethomebuttonenabled(true); }
where framelayout
container, have take in mainactivity
.
note : don't forget take toolbar
in home activity. because override mainactivity's toolbar.
edit posting whole code please refer it. although have took custom navigation drawer can work navigationview
too.
activity_main
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" > <relativelayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="false" > <!-- toolbar of mainactivity --> <include android:id="@+id/toolbar_headers" layout="@layout/toolbar_header" /> </relativelayout> <!-- here navigationview --> <com.random.extraclasses.scriminsetsframelayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:layout_marginright="-8dp" android:elevation="8dp" android:fitssystemwindows="true" app:insetforeground="#4000" > <include android:id="@+id/drawar_layout" layout="@layout/drawar_layout" /> </com.gujarat.property.extraclasses.scriminsetsframelayout>
mainactivity.java
public class mainactivity extends appcompatactivity implements onclicklistener { protected relativelayout framelayout; protected drawerlayout mdrawerlayout; protected listview mdrawerlist; protected scriminsetsframelayout mlayout; protected actionbardrawertoggle mdrawertoggle; protected static boolean islaunch = true; toolbar toolbar; private charsequence mtitle; private string[] navmenutitles; private typedarray navmenuicons; private arraylist<navdraweritem> navdraweritems; private navdrawerlistadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); navmenutitles = getresources().getstringarray(r.array.nav_drawer_items); navmenuicons = getresources() .obtaintypedarray(r.array.nav_drawer_icons); framelayout = (relativelayout) findviewbyid(r.id.frame_container); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.list); mlayout = (scriminsetsframelayout) findviewbyid(r.id.container); toolbar = (toolbar) findviewbyid(r.id.toolbar_headers); setsupportactionbar(toolbar); navdraweritems = new arraylist<navdraweritem>(); navdraweritems.add(new navdraweritem(navmenutitles[0], navmenuicons .getresourceid(0, -1))); navdraweritems.add(new navdraweritem(navmenutitles[1], navmenuicons .getresourceid(1, -1))); navdraweritems.add(new navdraweritem(navmenutitles[2], navmenuicons .getresourceid(2, -1))); navdraweritems.add(new navdraweritem(navmenutitles[3], navmenuicons .getresourceid(3, -1))); navmenuicons.recycle(); adapter = new navdrawerlistadapter(getapplicationcontext(), navdraweritems); mdrawerlist.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectedpos = position - 1; displayview(position); } }); mdrawerlist.setadapter(adapter); mdrawerlist.setfocusable(false); getsupportactionbar().setdisplayhomeasupenabled(true); getsupportactionbar().sethomebuttonenabled(true); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_drawer, r.string.app_name) { }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (islaunch) { islaunch = false; displayview(0); } } @override public boolean onoptionsitemselected(menuitem item) { if (mdrawertoggle.onoptionsitemselected(item)) { return true; } return super.onoptionsitemselected(item); } private void displayview(int position) { mdrawerlayout.closedrawer(mlayout); mainactivity.position = position; switch (position) { case 0: if (this.getclass().getsimplename().equals("home")) { } else { intent = new intent(this, home.class); startactivity(i); overridependingtransition(r.anim.fadein, r.anim.fadeout); } break; case 1: if (this.getclass().getsimplename().equals("forsale")) { } else { intent = new intent(this, forsale.class); startactivity(i); overridependingtransition(r.anim.fadein, r.anim.fadeout); } break; case 2: if (this.getclass().getsimplename().equals("forrent")) { } else { intent = new intent(this, forrent.class); startactivity(i); overridependingtransition(r.anim.fadein, r.anim.fadeout); } break; case 3: if (this.getclass().getsimplename().equals("newprojects")) { } else { intent = new intent(this, newprojects.class); startactivity(i); overridependingtransition(r.anim.fadein, r.anim.fadeout); } break; default: break; } } @override public void settitle(charsequence title) { mtitle = title; getsupportactionbar().settitle(mtitle); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); mdrawertoggle.onconfigurationchanged(newconfig); } @override public void onclick(view v) { } }
and home.java
public class home extends mainactivity implements onclicklistener { toolbar toolbar; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getlayoutinflater().inflate(r.layout.home, framelayout); toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); getsupportactionbar().setdisplayhomeasupenabled(true); getsupportactionbar().sethomebuttonenabled(true); getsupportactionbar().settitle(""); } @override public void onbackpressed() { super.onbackpressed(); if (mdrawerlayout.isdraweropen(mlayout)) { islaunch = false; mdrawerlayout.closedrawer(mlayout); } else { finishaffinity(); } }
and reference home.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" > <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar" android:background="@drawable/back51" > <textview android:id="@+id/etsearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="10dp" android:background="@android:color/transparent" android:text="search property" android:textcolor="#666666" /> </relativelayout> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollflags="scroll|enteralways" android:background="@color/toobar" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark" app:popuptheme="@style/themeoverlay.appcompat.light" > </android.support.v7.widget.toolbar> </relativelayout>
i hope out.
Comments
Post a Comment