java - how to include items in navigation drawer -



java - how to include items in navigation drawer -

here mainactivity.java

public class mainactivity extends actionbaractivity { // inisiasi toolbar private toolbar toolbar; // navigation drawer private drawerlayout drawerlayout; private actionbardrawertoggle drawertoggle; string title = "my app"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar = (toolbar) findviewbyid(r.id.toolbar); drawerlayout = (drawerlayout) findviewbyid(r.id.drawerlayout); if (toolbar != null) { toolbar.settitle(title); setsupportactionbar(toolbar); } initdrawer(); } /** * init navigation drawer thing */ private void initdrawer() { //setup navigation drawer drawertoggle = new actionbardrawertoggle(this, drawerlayout, toolbar, r.string.txt_open, r.string.txt_close) { @override public void ondrawerclosed(view drawerview) { super.ondrawerclosed(drawerview); // when drawer closed toolbar.settitle(title); } @override public void ondraweropened(view drawerview) { super.ondraweropened(drawerview); // when drawer open toolbar.settitle("nav menu"); } }; // setdrawerlisterner drawerlayout.setdrawerlistener(drawertoggle); } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); drawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); drawertoggle.onconfigurationchanged(newconfig); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { homecoming true; } if (drawertoggle.onoptionsitemselected(item)) homecoming true; homecoming super.onoptionsitemselected(item); } }

and here xml file layout view

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".mainactivity"> <!-- toolbar --> <include layout="@layout/toolbar" /> <!-- content --> <android.support.v4.widget.drawerlayout android:layout_width="match_parent" android:id="@+id/drawerlayout" android:layout_height="match_parent"> <relativelayout android:layout_width="match_parent" android:background="#fff" android:layout_height="match_parent"> <textview android:layout_centerinparent="true" android:layout_width="wrap_content" android:textcolor="#000" android:text="our content" android:layout_height="wrap_content" /> </relativelayout> <!-- nav drawer --> <linearlayout android:layout_gravity="start" android:orientation="vertical" android:background="#fff" android:layout_width="280dp" android:layout_height="match_parent"> <textview android:layout_width="wrap_content" android:textcolor="#000" android:text="nav drawer" android:layout_height="wrap_content" /> </linearlayout> </android.support.v4.widget.drawerlayout>

does know how add together listview navigation drawer. , want load web page when user selects each item on navigation drawer fragment. tried implement code sample comes many errors.

well, there nil afraid navigationdrawer. @ first may think special kind of activity , , that. much simple.

step 1 - create xml file 2 layouts

<framelayout> //main screen contents </framelayout> <linear/relative/listview/xyz> // layout u want shown in navigation drawer </linear/relative/listview/xyz>

step 2 - define ur drawer in activity class couple of codes

drawerlayout = (drawerlayout) findviewbyid(r.id.nvdrawer); drawertoggle = new actionbardrawertoggle(this, drawerlayout, r.drawable.ic_drawer, r.string.drawer_open, r.string.drawer_close); drawerlayout.setdrawerlistener(drawertoggle);

and override methods

@override public void ondrawerslide(view view, float v) { } @override public void ondraweropened(view view) { } @override public void ondrawerclosed(view view) { } @override public void ondrawerstatechanged(int i) { } @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); drawertoggle.syncstate(); }

step 3 - add together code in onoptionsitemselected

if (drawertoggle.onoptionsitemselected(item)) { homecoming true;

java android drawer

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -