android - Use Custom Layout in NavigationDrawer With Header And list -


how add custom layout in navigationview , design create custom navigationview use material design,i want put drawer icon right , text left of this

enter image description here

i search , experience works fine

at first create layout header. name nav_header_main.xml put in layouts folders in res , put code in it..

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"     android:background="@drawable/header"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:theme="@style/themeoverlay.appcompat.dark"     android:gravity="top">      <relativelayout         android:layout_width="match_parent"         android:layout_height="wrap_content"          android:padding="16dp">         <de.hdodenhof.circleimageview.circleimageview             android:id="@+id/cv_nave_profile_image"             android:layout_width="@dimen/nav_profile_image"             android:layout_height="@dimen/nav_profile_image"             android:layout_alignparentright="true"             android:layout_alignparenttop="true"             android:src="@drawable/profile"             />          <linearlayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:layout_toleftof="@id/cv_nave_profile_image"             android:layout_alignparenttop="true"             android:padding="@dimen/activity_horizontal_margin"             android:orientation="vertical"              >             <textview                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:id="@+id/tv_nav_name"                 android:textstyle="bold"                 android:typeface="sans"                 android:textcolor="#ffffff"                 android:gravity="right"                 android:layout_gravity="right"                 android:text="رخداد جدید"                 android:paddingbottom="5dp"                 android:textsize="@dimen/body"                 />          </linearlayout>         <textview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_alignparentbottom="true"             android:typeface="sans"             android:textcolor="#ffffff"             android:id="@+id/tv_nav_phone"             android:layout_alignparentleft="true"             android:text="0370077315"             />      </relativelayout>   </linearlayout> 

then include child of navigationview , menu item use recyclerview show menu , icon navigationview

<android.support.design.widget.coordinatorlayout     android:layout_width="match_parent"     android:layout_height="match_parent"     android:fitssystemwindows="true"     tools:context="spydroid.ir.dorobar.activities.searchactivity">      <android.support.design.widget.appbarlayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:theme="@style/apptheme.appbaroverlay">          <android.support.v7.widget.toolbar             android:id="@+id/toolbar"             android:layout_width="match_parent"             android:layout_height="?attr/actionbarsize"             android:background="?attr/colorprimary"             app:popuptheme="@style/apptheme.popupoverlay">           </android.support.v7.widget.toolbar>      </android.support.design.widget.appbarlayout>      <include layout="@layout/content_search" />      <android.support.design.widget.floatingactionbutton         android:id="@+id/fab"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="bottom|end"         android:layout_margin="@dimen/fab_margin"         android:src="@android:drawable/ic_dialog_email" />  </android.support.design.widget.coordinatorlayout> <android.support.design.widget.navigationview android:id="@+id/nav_view"     android:layout_width="fill_parent" android:layout_height="match_parent"     android:layout_gravity="right" android:fitssystemwindows="true"     android:layout_marginleft="@dimen/nav_margin"     >     <linearlayout         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:orientation="vertical"         >         <include layout="@layout/nav_header_main" />         <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"             android:layout_width="match_parent"             android:layout_height="match_parent">             <android.support.v7.widget.recyclerview                 android:id="@+id/drawer_slidermenu"                 android:layout_width="fill_parent"                 android:layout_height="match_parent"                 android:layout_margintop="16dp"/>          </relativelayout>     </linearlayout>  </android.support.design.widget.navigationview> 

have remember put navigationview in drawerlayout

then create layout menu item imageview , textview layout , name card_drawer_item.xml , code here

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="48dp">      <imageview         android:id="@+id/drawer_icon"         android:layout_width="25dp"         android:layout_height="wrap_content"         android:layout_alignparentright="true"         android:layout_marginleft="12dp"         android:layout_marginright="12dp"         android:src="@drawable/ic_about"         android:layout_centervertical="true" />      <textview         android:id="@+id/drawer_text"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_toleftof="@id/drawer_icon"         android:minheight="?android:attr/listpreferreditemheightsmall"         android:textappearance="?android:attr/textappearancelistitemsmall"         android:gravity="center_vertical"         android:typeface="sans"         android:paddingright="40dp"/>  </relativelayout> 

then create viewholder folder layout.

public class draweritemholder extends recyclerview.viewholder {      public imageview  itemicon;     public textview itemtext;     public draweritemholder(view itemview) {         super(itemview);         itemicon= (imageview) itemview.findviewbyid(r.id.drawer_icon);         itemtext= (textview) itemview.findviewbyid(r.id.drawer_text);     } } 

now define text of menu items string array , array contains menu icons in menu in strings.xml

<string-array name="drawer_items">         <item>setting</item>         <item>add record</item>         <item>ads</item>         <item>about</item>         <item>call</item>         <item>help</item>         <item>privacy</item>     </string-array>     <array name="drawers_icons">         <item>@drawable/ic_action_settings</item>         <item>@drawable/ic_plus</item>         <item>@drawable/ic_ads</item>         <item>@drawable/ic_about</item>          <item>@drawable/ic_phone</item>         <item>@drawable/ic_help</item>         <item>@drawable/ic_policy</item>     </array> 

then need adapter

public class draweritemadapter extends recyclerview.adapter<draweritemholder> {      // slide menu items     private list<draweritem> items;     private list<integer> drawericons;     public draweritemadapter(list<draweritem>  items) {         super();         this.items = items;       }      @override     public draweritemholder oncreateviewholder(viewgroup parent, int viewtype) {         view itemview = layoutinflater.                 from(parent.getcontext()).                 inflate(r.layout.card_drawer_item, parent, false);          return new draweritemholder(itemview);     }      @override     public void onbindviewholder(draweritemholder holder, int position) {         holder.itemicon.setimageresource(items.get(position).geticonid());         holder.itemtext.settext(items.get(position).gettext());     }      @override     public int getitemcount() {         return items.size();     } } 

every thing ok .. have set navigationview in activity.

protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_search);         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);            drawer = (drawerlayout) findviewbyid(r.id.drawer_layout);             reclist = (recyclerview) findviewbyid(r.id.drawer_slidermenu);         reclist.sethasfixedsize(true);         linearlayoutmanager llm = new linearlayoutmanager(this);         llm.setorientation(linearlayoutmanager.vertical);         reclist.setlayoutmanager(llm);          string []itemstitle=getresources().getstringarray(r.array.drawer_items);         typedarray icons=getresources().obtaintypedarray(r.array.drawers_icons);         list<draweritem>draweritems= new  arraylist<draweritem>();         for(int i=0;i<itemstitle.length;i++){                 draweritems.add(new draweritem(icons.getresourceid(i,-1),itemstitle[i]));         }          draweritemadapter ad= new draweritemadapter(draweritems);         reclist.setadapter(ad);      }       @override     public void onbackpressed() {          if (drawer.isdraweropen(gravitycompat.end)) {             drawer.closedrawer(gravitycompat.end);             return;         }         super.onbackpressed();     } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -