NavigationDrawerExample from developer.android, insted of planets, load webviews -
i downloaded navigation drawer example developer.android.com. , there example planets. want instead load images planets, load webviews url. don;t know how that. here code. sugestions ?
package com.example.android.navigationdrawerexample; import java.util.locale; import android.app.activity; import android.app.fragment; import android.app.fragmentmanager; import android.app.searchmanager; import android.content.intent; import android.content.res.configuration; import android.os.bundle; import android.support.v4.app.actionbardrawertoggle; import android.support.v4.view.gravitycompat; import android.support.v4.widget.drawerlayout; import android.view.layoutinflater; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.webkit.webview; import android.webkit.webviewclient; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.listview; import android.widget.toast; public class mainactivity extends activity { private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; private string[] mplanettitles; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtitle = mdrawertitle = gettitle(); mplanettitles = getresources().getstringarray(r.array.planets_array); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); // set custom shadow overlays main content when drawer opens mdrawerlayout.setdrawershadow(r.drawable.drawer_shadow, gravitycompat.start); // set drawer's list view items , click listener mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mplanettitles)); mdrawerlist.setonitemclicklistener(new draweritemclicklistener()); // enable actionbar app icon behave action toggle nav drawer getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); // actionbardrawertoggle ties the proper interactions // between sliding drawer , action bar app icon mdrawertoggle = new actionbardrawertoggle( this, /* host activity */ mdrawerlayout, /* drawerlayout object */ r.drawable.ic_drawer, /* nav drawer image replace 'up' caret */ r.string.drawer_open, /* "open drawer" description accessibility */ r.string.drawer_close /* "close drawer" description accessibility */ ) { public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { selectitem(0); } } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main, menu); return super.oncreateoptionsmenu(menu); } /* called whenever call invalidateoptionsmenu() */ @override public boolean onprepareoptionsmenu(menu menu) { // if nav drawer open, hide action items related content view boolean draweropen = mdrawerlayout.isdraweropen(mdrawerlist); menu.finditem(r.id.action_websearch).setvisible(!draweropen); return super.onprepareoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // action bar home/up action should open or close drawer. // actionbardrawertoggle take care of this. if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle action buttons switch(item.getitemid()) { case r.id.action_websearch: // create intent perform web search planet intent intent = new intent(intent.action_web_search); intent.putextra(searchmanager.query, getactionbar().gettitle()); // catch event there's no activity handle intent if (intent.resolveactivity(getpackagemanager()) != null) { startactivity(intent); } else { toast.maketext(this, r.string.app_not_available, toast.length_long).show(); } return true; default: return super.onoptionsitemselected(item); } } /* click listner listview in navigation drawer */ private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position); } } private void selectitem(int position) { // update main content replacing fragments fragment fragment = new planetfragment(); bundle args = new bundle(); args.putint(planetfragment.arg_planet_number, position); fragment.setarguments(args); fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.content_frame, fragment).commit(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); settitle(mplanettitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } @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); } /** * fragment appears in "content_frame", shows planet */ public static class planetfragment extends fragment { public static final string arg_planet_number = "planet_number"; public planetfragment() { // empty constructor required fragment subclasses } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_planet, container, false); int = getarguments().getint(arg_planet_number); string planet = getresources().getstringarray(r.array.planets_array)[i]; int imageid = getresources().getidentifier(planet.tolowercase(locale.getdefault()), "drawable", getactivity().getpackagename()); ((imageview) rootview.findviewbyid(r.id.image)).setimageresource(imageid); getactivity().settitle(planet); return rootview; } } }
activity_main.xml
<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"> <!-- main content view, view below consumes entire space available using match_parent in both dimensions. --> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- android:layout_gravity="start" tells drawerlayout treat sliding drawer on left side left-to-right languages , on right side right-to-left languages. drawer given fixed width in dp , extends full height of container. solid background used contrast content view. --> <listview android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="0dp" android:background="#111"/> </android.support.v4.widget.drawerlayout>
drawer_list.xml
<textview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelistitemsmall" android:gravity="center_vertical" android:paddingleft="16dp" android:paddingright="16dp" android:textcolor="#fff" android:background="?android:attr/activatedbackgroundindicator" android:minheight="?android:attr/listpreferreditemheightsmall"/>
fragment_planet.xml
<imageview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:gravity="center" android:padding="32dp" />
values > string.xml
<resources> <string name="app_name">navigation drawer example</string> <string-array name="planets_array"> <item>mercury</item> <item>venus</item> <item>earth</item> <item>mars</item> <item>jupiter</item> <item>saturn</item> <item>uranus</item> <item>neptune</item> </string-array> <string name="drawer_open">open navigation drawer</string> <string name="drawer_close">close navigation drawer</string> <string name="action_websearch">web search</string> <string name="app_not_available">sorry, there\'s no web browser available</string> </resources>
Comments
Post a Comment