navigation header layout cutoff from top
Asked Answered
P

4

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>
Parasite answered 14/5, 2016 at 8:21 Comment(2)
you have to put navigation view below toolbar using layout_below propertyStarlight
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 sayingParasite
P
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);
        }
Parasite answered 14/5, 2016 at 10:9 Comment(0)
F
38

add your NavigationView android:fitsSystemWindows="false"

Flyaway answered 17/4, 2017 at 11:50 Comment(2)
Perfect Answer. ThanksCholesterol
thank you it worked great , really the perfect answerRanson
P
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);
        }
Parasite answered 14/5, 2016 at 10:9 Comment(0)
S
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"
   />
Selfdeceit answered 14/5, 2016 at 10:17 Comment(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.Parasite
R
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.

Rumanian answered 11/4, 2021 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.