java - How to add navigation drawer on pre built app? -
i new android development. have built app without navigation drawer. now, want add navigation drawer same across activities in app. me on issue.
assuming original activities extending 'appcompatactivity', make base activity following , extend other activities 'baseactivity'. baseactivity layout content drawaerlayout , 1 framelayout. inside baseactivity ovveride setcontentview method , inflat activity layout inside framelayout.
baseactivity.java
public class baseactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { private framelayout baselayout; public actionbardrawertoggle drawertoggle; public toolbar toolbar; @override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); super.setcontentview(r.layout.base_layout); toolbar = (toolbar) findviewbyid( r.id.toolbar); setsupportactionbar(toolbar); baselayout = (framelayout) findviewbyid(r.id.base_view); navigationview navigationview = (navigationview) findviewbyid(r.id.navigation_view); drawerlayout drawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); navigationview.setnavigationitemselectedlistener(this); drawertoggle = new actionbardrawertoggle(this, drawerlayout, toolbar, 0, 0); drawerlayout.adddrawerlistener(drawertoggle); } @override public void setcontentview(view view) { if (baselayout != null) { viewgroup.layoutparams params = new viewgroup.layoutparams( viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); baselayout.addview(view, params); } } @override public void setcontentview(view view, viewgroup.layoutparams params) { if (baselayout != null) { baselayout.addview(view, params); } } @override public boolean onnavigationitemselected(@nonnull menuitem item) { //todo return false; } }
base_layout.xml
<?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"> <linearlayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.appbarlayout style="@style/widget.myapp.toolbar.solid" android:layout_width="match_parent" android:layout_height="wrap_content" > <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary"> </android.support.v7.widget.toolbar> </android.support.design.widget.appbarlayout> <framelayout android:id="@+id/base_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </linearlayout> <android.support.design.widget.navigationview android:id="@+id/navigation_view" android:layout_width="280sp" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/base_header" app:menu="@menu/drawer" /> </android.support.v4.widget.drawerlayout>
base_header.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/blank" android:orientation="vertical"> <imageview android:id="@+id/nav_header_icon" android:layout_width="50sp" android:layout_height="50sp" android:layout_marginbottom="8dp" android:layout_marginstart="16sp" android:contentdescription="@null" android:src="@mipmap/ic_launcher" app:layout_constraintbottom_totopof="@+id/nav_header_title" app:layout_constraintstart_tostartof="parent" /> <textview android:id="@+id/nav_header_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginend="16sp" android:layout_marginstart="16sp" android:fontfamily="sans-serif-medium" android:text="@string/app_name" app:layout_constraintbottom_totopof="@+id/nav_header_subtitle" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="parent" /> <textview android:id="@+id/nav_header_subtitle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginbottom="8dp" android:layout_marginend="16sp" android:layout_marginstart="16sp" android:fontfamily="sans-serif" android:text="@string/app_name" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintend_toendof="parent" app:layout_constraintstart_tostartof="parent" /> </android.support.constraint.constraintlayout>
add drawer.xml in "menu" folder in resources.
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" android:checkablebehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/icon_home" android:title="home" /> </menu>
Comments
Post a Comment