15

i am using navigation view with expandable listview. i am adding header layout programmatically. but it cut off from top. if i give header layout height to 240dp than it looks ok but in lower version it take to much height. heres my header layout xml which i am inflating to header of expandable listview.

navigation_header

<?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="wrap_content"
              android:background="@color/colorPrimary"
              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"
    >

    <com.lib.CircleImageView
        android:id="@+id/imageViewHeaderPerson"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon"/>

    <TextView
        android:id="@+id/textViewName"
        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.Subhead"
        android:textColor="@color/DEFAULT"/>

    <TextView
        android:id="@+id/textViewCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Base.TextAppearance.AppCompat.Body1"
        android:text="heelo"
        android:textColor="@color/DEFAULT"/>

</LinearLayout>

and my navigation layout having drawer

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
           />

    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        >
    <ExpandableListView
        android:groupIndicator="@null"
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:headerDividersEnabled="false"
        android:childDivider="#00000000"


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

</android.support.v4.widget.DrawerLayout>
2
  • 1
    you have to put navigation view below toolbar using layout_below property Commented May 14, 2016 at 8:23
  • i am not using toolbar. it is a action bar . i am supplying null in toobar ` actionBarDrawerToggle = new ActionBarDrawerToggle( this, drawerLayout, null, navigationConfig.getDrawerOpenDesc(), navigationConfig.getDrawerCloseDesc() ) ` can you code snippet for what you are saying Commented May 14, 2016 at 8:26

4 Answers 4

38

add your NavigationView android:fitsSystemWindows="false"

2
  • 1
    Perfect Answer. Thanks
    – shyam.y
    Commented Oct 5, 2018 at 2:22
  • 1
    thank you it worked great , really the perfect answer
    – Mosa
    Commented Feb 9, 2021 at 21:40
1

Add top margin of action bar size in your layout, like

<ExpandableListView
    android:groupIndicator="@null"
    android:id="@+id/left_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:headerDividersEnabled="false"
    android:layout_marginTop="?attr/actionBarSize"
    android:childDivider="#00000000"
   />
1
  • android:layout_marginTop="?attr/actionBarSize" but it supports only above api 11. i am using min 8 api level. do you know alternate of this. Commented May 14, 2016 at 10:20
1

i solved this by using this. thanks for the comment to set navigation view below toobar.

   // Calculate ActionBar height
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            TypedValue tv = new TypedValue();
            int actionBarHeight = 0;

            if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources()
                        .getDisplayMetrics());
            }
            ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) expandableListView
                    .getLayoutParams();

            mlp.setMargins(0, actionBarHeight, 0, 0);
            expandableListView.setLayoutParams(mlp);
        }
1

I added the android:fitsSystemWindows="false" attribute to the <com.google.android.material.navigation.NavigationView> inside my <androidx.drawerlayout.widget.DrawerLayout>

And then manually added some padding or margin to the <com.google.android.material.navigation.NavigationView> to adjust the header position correctly.

Now the Header remains at same place in both "Notched" and "Non-Notched" displays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.