android - Navigation view with fragment. Toolbar -


so, have activity navigation view. click on item change fragment in activity. fragment have same toolbar. 1 have toolbar , tablayout it. know better declare toolbar once on activity

<relativelayout     android:layout_width="match_parent"     android:layout_height="match_parent">      <include layout="@layout/toolbar" />      <framelayout         android:id="@+id/container"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_below="@+id/toolbar" />  </relativelayout> 

or declare in each fragment.

the disadvantage of first method default toolbar shadow. when add tabs in fragment, shadow looks

enter image description here

when tried 2 solution. toolbar icon instead drawer animated logo.

thanks.

i had exact same problem. how solved it:

  • move toolbars fragments suggested (so won't have shadow separating two). allows way more flexible way implement (different) toolbars in layouts too.
  • replace toolbar's navigation icon custom 1 this:

    toolbar.setnavigationicon(r.drawable.ic_action_menu); 

(i used android asset studio create icon preferred color)

  • now open navigationview new menu(home) icon. can through mainactivity (the 1 navigationview). create public method in activity opens drawer:

    public void opendrawer(){     mdrawerlayout.opendrawer(gravity.left); } 
  • now call method in onoptionsitemselected in fragments this:

    @override public boolean onoptionsitemselected(menuitem item) {     // handle item selection     switch (item.getitemid()) {         case android.r.id.home: //menu icon             ((mainactivity)getactivity()).opendrawer();             return true;                     default:             return super.onoptionsitemselected(item);     } } 

that's it. of course downside must implement toolbar in each fragment. however, way (that know of) enables have toolbar (+tablayout) in fragment , still able control navigationview.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -