java - Add icon in Navigatior drawer.. (Follow from android hive) -
im using http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ create navigation drawer, how add icon in every title have? please thanks
btw im using fragment
fragmentdrawer.java
public static list<navdraweritem> getdata() { list<navdraweritem> data = new arraylist<>(); // preparing navigation drawer items (int = 0; < titles.length; i++) { navdraweritem navitem = new navdraweritem(); navitem.settitle(titles[i]); data.add(navitem); } return data; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // drawer labels titles = getactivity().getresources().getstringarray(r.array.nav_drawer_labels); icons = getactivity().getresources().getintarray(r.array.nav_drawer_icons); }
string.xml
<string name="nav_item_home">home</string> <string name="nav_item_friends">friends</string> <string name="nav_item_notifications">messages</string> <string name="nav_item_calculator">bmi calculator</string> <string name="nav_item_listview">test json</string> <string name="nav_item_testabs">test tabs</string> <!-- navigation drawer item labels --> <string-array name="nav_drawer_labels"> <item>@string/nav_item_home</item> <item>@string/nav_item_friends</item> <item>@string/nav_item_notifications</item> <item>@string/nav_item_calculator</item> <item>@string/nav_item_listview</item> <item>@string/nav_item_testabs</item> </string-array> <array name="nav_drawer_icons"> <item>@drawable/arm1</item> <item>@drawable/abs2</item> <item>@drawable/back</item> <item>@drawable/leg</item> <item>@drawable/shoulder</item> <item>@drawable/achest</item> </array>
mainactivity.java
private void displayview(int position) { fragment fragment = null; string title = getstring(r.string.app_name); switch (position) { case 0: fragment = new homefragment(); title = getstring(r.string.nav_item_home); break; case 1: fragment = new friendsfragment(); title = getstring(r.string.nav_item_friends); break; case 2: fragment = new messagesfragment(); title = getstring(r.string.nav_item_notifications); break; case 3: fragment = new calculatorfragment(); title = getstring(r.string.nav_item_calculator); break; case 4: fragment = new moviefragment(); title = getstring(r.string.nav_item_listview); break; case 5: fragment = new tabfragment(); title = getstring(r.string.nav_item_testabs); default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.container_body, fragment); fragmenttransaction.commit(); // set toolbar title getsupportactionbar().settitle(title); } }
navdrawweritem.java
public class navdraweritem { private boolean shownotify; private string title; private int icon; public navdraweritem() { } public navdraweritem(boolean shownotify, string title, int icon) { this.shownotify = shownotify; this.title = title; this.icon = icon; } public boolean isshownotify() { return shownotify; } public void setshownotify(boolean shownotify) { this.shownotify = shownotify; } public string gettitle() { return title; } public int geticon(){ return this.icon; } public void settitle(string title) { this.title = title; } public void seticon(int icon){ this.icon = icon; }
easiest way use navigationview in support lib.
see link demo
you have create menu file item navigation
<group android:checkablebehavior="single"> <item android:id="@+id/inbox" android:checked="false" android:icon="@drawable/ic_inbox_black" android:title="@string/inbox_string" /> <item android:id="@+id/starred" android:checked="false" android:icon="@drawable/ic_star_black" android:title="@string/starred_string" /> <item android:id="@+id/sent_mail" android:checked="false" android:icon="@drawable/ic_send_black" android:title="@string/sent_mail_string" /> <item android:id="@+id/drafts" android:checked="false" android:icon="@drawable/ic_drafts_black" android:title="@string/draft_string" /> <item android:id="@+id/allmail" android:checked="false" android:icon="@drawable/ic_email_black" android:title="@string/all_mail_string" /> <item android:id="@+id/trash" android:checked="false" android:icon="@drawable/ic_delete_black" android:title="@string/trash_string" /> <item android:id="@+id/spam" android:checked="false" android:icon="@drawable/ic_error_black" android:title="@string/spam_string" /> </group>
Comments
Post a Comment