0

I have done my best to reproduce a UI design for an android navigation drawer. I have already designed the nav header to look like what is in the UI design. When I run the app, the header (the layout in nav_header_home.xml) is not visible in the simulator.

The nav_header_home.xml for the navigationdrawer:

<?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="wrap_content"
    android:background="@color/clientPrimaryBlue"
    android:gravity="center"
    android:orientation="horizontal"
    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="@drawable/home" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Home"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

</LinearLayout>

activity_home_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <item android:title="Movies">
        <menu>
            <item
                android:id="@+id/nav_action"
                android:icon="@drawable/ic_menu_share"
                android:title="Action" />
            <item
                android:id="@+id/nav_adventure"
                android:icon="@drawable/ic_menu_send"
                android:title="Adventure" />
            <item
                android:id="@+id/nav_comedy"
                android:icon="@drawable/ic_menu_send"
                android:title="Comedy" />
            <item
                android:id="@+id/nav_crime"
                android:icon="@drawable/ic_menu_send"
                android:title="Crime" />
            <item
                android:id="@+id/nav_drama"
                android:icon="@drawable/ic_menu_send"
                android:title="Drama" />
            <item
                android:id="@+id/nav_fantasy"
                android:icon="@drawable/ic_menu_send"
                android:title="Fantasy" />
        </menu>
    </item>
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_actors"
            android:icon="@drawable/ic_menu_camera"
            android:title="Actors" />
        <item
            android:id="@+id/nav_directors"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Directors" />
        <item
            android:id="@+id/nav_producers"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Producers" />
    </group>

    <item android:title="My Account">
        <menu>
            <item
                android:id="@+id/nav_update_profile"
                android:icon="@drawable/ic_menu_share"
                android:title="Update Profile" />
            <item
                android:id="@+id/nav_reset_password"
                android:icon="@drawable/ic_menu_send"
                android:title="Reset Password" />
            <item
                android:id="@+id/nav_sign_out"
                android:icon="@drawable/ic_menu_send"
                android:title="Sign Out" />
        </menu>
    </item>

</menu>

Screenshot of expected menu layout

enter image description here

Screenshot of layout in android emulator

enter image description here

app_bar_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="media.client.clientmediaandroid.activity.home.HomeActivity">

    <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.design.widget.AppBarLayout>

    <include layout="@layout/content_home" />

    <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"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

action_home.xml with the DrawerLayout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_home"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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_home"
        app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>
James Z
  • 12,209
  • 10
  • 24
  • 44
sage
  • 587
  • 1
  • 8
  • 28
  • Post screen shots of output and expected output – Vidhi Dave Mar 12 '18 at 12:46
  • post your `navigationView xml` code (activity xml) – Santanu Sur Mar 12 '18 at 12:47
  • OK, I will. Thank you very much. – sage Mar 12 '18 at 12:48
  • 1
    Did you forget to set the header layout in `NavigationView`? `app:headerLayout="@layout/nav_header"` – denvercoder9 Mar 12 '18 at 12:50
  • I did not touch anything except for customizing the header by replacing the icon there with my own home icon and text – sage Mar 12 '18 at 12:58
  • buddy where is header layout – Vishal Yadav Mar 12 '18 at 13:05
  • Post your activity layout – denvercoder9 Mar 12 '18 at 13:12
  • I have added the screenshots – sage Mar 12 '18 at 13:23
  • @Vishal Yadav, The header layout is already in the post. – sage Mar 12 '18 at 13:25
  • I have posted the app_bar_home.xml layout file although it doesn't have anything to do with the issue as I have not changed it from the default one generated by Android Studio. – sage Mar 12 '18 at 13:26
  • I have added the activity_home.xml with the DrawerLayout to the post. – sage Mar 12 '18 at 13:33
  • 1
    Looks like it's there, but just shorter than you're expecting, and hidden under the status bar. To test, set the `fitsSystemWindows` attributes to `false` on the `DrawerLayout` and `NavigationView`. – Mike M. Mar 26 '18 at 06:00
  • @MikeM., thank you very much. I will check do as you have said and revert. Thank you very much. – sage Mar 26 '18 at 07:03
  • @MikeM., and you are right. I can see it in design view but prior to your answer, my challenge was getting it to show. Let me do as above and revert to you. – sage Mar 26 '18 at 07:08
  • @MikeM., Thank you very much. It is showing up now although the height is not as much as I want it to be. Is there a way to make it clickable as a Menu. It does not appear in the default list of Navigation menu items but rather in the nav header. Do I need to reference its layout and add an onClickListener like is done for buttons? – sage Mar 26 '18 at 07:42
  • Yeah, you'll have to set your own `OnClickListener` on it. Since it's in the `NavigationView` header, you'll have to find it a little differently in `onCreate()`, though. Have a look at [this answer](https://stackoverflow.com/a/33699825) to see how that's done. If you want the whole thing clickable, then just set the `OnClickListener` right on `header` in that example. – Mike M. Mar 26 '18 at 07:46
  • @MikeM., thank you very much. I will mark this question as answered. I appreciate your quick responses a lot. Cheers. – sage Mar 26 '18 at 08:14
  • 4
    Does this answer your question? [NavigationView get/find header layout](https://stackoverflow.com/questions/33194594/navigationview-get-find-header-layout) – STA Mar 07 '21 at 11:25

2 Answers2

0

With help from @MikeM., I was able to adjust my layout settings in a way that allowed me to see the navigation header.

His contribution can be read directly here - Home Navigation header of NavigationDrawer not showing in simulator

sage
  • 587
  • 1
  • 8
  • 28
0

Set the fitsSystemWindows attributes to false on the DrawerLayout and NavigationView as suggested by Mike. M.