One of the things that I don’t like in Android programming is a lot of files corresponding to one element (for example, Hamburger menu). Well, or maybe it's in the Android studio templates like this. Anyway, in this question I want to find out how you can put the Hamburger menu from the Android Studio template Navigation Drawer Activity into one file, but it will be useful in other situations when there are too many files. At the moment, the Hamburger menu is "assembled" from at least 3 files:

nav_header_main.xml - top of the pull-out menu:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" 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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" app:srcCompat="@android:drawable/sym_def_app_icon" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="android.studio@android.com" /> </LinearLayout> 
  • side_nav_bar.xml - fill the background of the upper part of the menu (the code does not want to be displayed):

  • activity_main_drawer.xml - menu items:

     <group android:checkableBehavior="single"> <item android:id="@+id/nav_camera" android:icon="@drawable/ic_menu_camera" android:title="Import" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="Gallery" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow" /> <item android:id="@+id/nav_manage" android:icon="@drawable/ic_menu_manage" android:title="Tools" /> </group> <item android:title="Communicate"> <menu> <item android:id="@+id/nav_share" android:icon="@drawable/ic_menu_share" android:title="Share" /> <item android:id="@+id/nav_send" android:icon="@drawable/ic_menu_send" android:title="Send" /> </menu> </item> 

It is advisable to put all this nav_header_main.xml code in nav_header_main.xml , then we can call it from activity_main.xml (as it is done now):

 <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> 

So, I repeat the question: how to merge the xml-code from the three above given files into nav_header_main.xml ?

  • No, the hierarchy does not allow. The hml menu must be in the hml menu, otherwise there would be no point in the separation. Everything in the mess is no better than three files. - Silento
  • Well, how to say ... There is a serious functional application, then there may be hundreds of such files ... If this menu alone gives 3 additional files. - Lateral Gleb

0