0

I am implementing navigation view and its has header view how to hide the image view dynamically

Below is my code

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

drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/drawer_logo_icon"
    android:layout_width="150dp"
    android:layout_height="0px"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:layout_weight="2"
    android:background="@drawable/logo_white" />

<TextView
    android:id="@+id/drawer_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/drawer_phone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/drawer_header_text"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:visibility="invisible" />

To get the menu item i can use navigationview.getmenuID(). But how to hide the header layout image programmatically

John
  • 1,407
  • 7
  • 26
  • 51

1 Answers1

1

Assign an id to the root layout of your drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_header_layout"
    android:layout_width="match_parent"
    .............

Then simply hide it by getting its reference in code.

RelativeLayout drawerHeaderLayout = (RelativeLayout) findViewById(R.id.drawer_header_layout);
drawerHeaderLayout.setVisibility(View.GONE);

That will hide your Navigation View header. (Works for me)

EDIT: (Better way)

Another way to remove the header completely is removeHeaderView

 NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
 view.removeHeaderView(drawerHeaderLayout);
kushpf
  • 1,078
  • 10
  • 22