android - How can I call one of my activity using navigation drawer ? -
i have navigation drawer activity mainactivity class , have homepage.class activity made. since navigation drawer uses fragments, unable call homepage class through drawer. plz me.
here code drawer-
public class mainactivity extends activity { 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 adapter; @suppresslint("newapi") @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))); // photos navdraweritems.add(new navdraweritem(navmenutitles[2], navmenuicons.getresourceid(2, -1))); // communities, add counter here navdraweritems.add(new navdraweritem(navmenutitles[3], navmenuicons.getresourceid(3, -1), true, "22")); // pages navdraweritems.add(new navdraweritem(navmenutitles[4], navmenuicons.getresourceid(4, -1))); // what's hot, add counter here navdraweritems.add(new navdraweritem(navmenutitles[5], navmenuicons.getresourceid(5, -1), true, "50+")); // recycle typed array navmenuicons.recycle(); mdrawerlist.setonitemclicklistener(new slidemenuclicklistener()); // setting nav drawer list adapter adapter = new navdrawerlistadapter(getapplicationcontext(), navdraweritems); mdrawerlist.setadapter(adapter); // enabling action bar app icon , behaving toggle button getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.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) { getactionbar().settitle(mtitle); // calling onprepareoptionsmenu() show action bar icons invalidateoptionsmenu(); } public void ondraweropened(view drawerview) { getactionbar().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); } } /** * slide menu item click listener * */ private 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.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // toggle nav drawer on selecting action bar app icon/title if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle action bar actions click switch (item.getitemid()) { case r.id.action_settings: return true; default: return super.onoptionsitemselected(item); } } /* * * called when invalidateoptionsmenu() triggered */ @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); } /** * diplaying fragment view selected nav drawer list item * */ 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; case 2: fragment = new photosfragment(); break; case 3: fragment = new communityfragment(); break; case 4: fragment = new pagesfragment(); break; case 5: fragment = new whatshotfragment(); 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; getactionbar().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(configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggls mdrawertoggle.onconfigurationchanged(newconfig); }
}
one of fragments -
public class homefragment extends fragment { public homefragment(){} @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { intent intent = new intent(); intent.setclass(getactivity(), homepage.class); startactivity(intent); view rootview = inflater.inflate(r.layout.activity_main, container, false); return rootview; }
homepage activity want call when item on navigation drawer clicked :
public class homepage extends mainactivity { private static final string tag = mainactivity.class.getsimplename(); private listview listview; private feedlistadapter listadapter; private list<feeditem> feeditems; private string url_feed = "https://gist.githubusercontent.com/anonymous/e6c3336c57bf40c794ab/raw/022058ac0ceafd15bfed89a72e6b741dd84da130/blob.json"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fragment_home); listview = (listview) findviewbyid(r.id.list); feeditems = new arraylist<feeditem>(); listadapter = new feedlistadapter(this, feeditems); listview.setadapter(listadapter); // these 2 lines not needed, // of facebook (changing background color & hiding icon) getactionbar().setbackgrounddrawable(new colordrawable(color.parsecolor("#ef7b00"))); // getactionbar().seticon( // new colordrawable(getresources().getcolor(android.r.color.transparent))); // first check cached request cache cache = appcontroller.getinstance().getrequestqueue().getcache(); entry entry = cache.get(url_feed); if (entry != null) { // fetch data cache try { string data = new string(entry.data, "utf-8"); try { parsejsonfeed(new jsonobject(data)); } catch (jsonexception e) { e.printstacktrace(); } } catch (unsupportedencodingexception e) { e.printstacktrace(); } } else { // making fresh volley request , getting json jsonobjectrequest jsonreq = new jsonobjectrequest(method.get, url_feed, null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { volleylog.d(tag, "response: " + response.tostring()); if (response != null) { parsejsonfeed(response); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { volleylog.d(tag, "error: " + error.getmessage()); } }); // adding request volley request queue appcontroller.getinstance().addtorequestqueue(jsonreq); } } /** * parsing json reponse , passing data feed view list adapter * */ private void parsejsonfeed(jsonobject response) { try { jsonarray feedarray = response.getjsonarray("feed"); (int = 0; < feedarray.length(); i++) { jsonobject feedobj = (jsonobject) feedarray.get(i); feeditem item = new feeditem(); item.setid(feedobj.getint("id")); item.setname(feedobj.getstring("name")); // image might null string image = feedobj.isnull("image") ? null : feedobj .getstring("image"); item.setimge(image); item.setstatus(feedobj.getstring("status")); item.setprofilepic(feedobj.getstring("profilepic")); item.settimestamp(feedobj.getstring("timestamp")); // url might null string feedurl = feedobj.isnull("url") ? null : feedobj .getstring("url"); item.seturl(feedurl); feeditems.add(item); } // notify data changes list adapater listadapter.notifydatasetchanged(); } catch (jsonexception e) { e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } }
xml drawer:
<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 display fragments --> <framelayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent"> </framelayout> <!-- listview display slider menu --> <listview android:id="@+id/list_slidermenu" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/list_background" android:choicemode="singlechoice" android:divider="@color/list_divider" android:dividerheight="1dp" android:listselector="@drawable/list_selector" /> </android.support.v4.widget.drawerlayout>
and xml activity want called -
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <listview android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="@null" /> </linearlayout>
the api starting activity not change when using navigation drawer. still need call startactivity()
launch different activity.
in case, code this:
private void displayview(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: // or whatever nav item position should start activity startactivity(new intent(this, homepage.class)); return; // rest of cases }
note when after calling startactivity()
homepage item, return
instead of break
displayview()
doesn't try replace displayed fragment.
Comments
Post a Comment