0

I would like to simply change the text in my DrawerLayout But everything I try achieve to a NullPointerException

Can someone help me ? I can't succeed to go trough the layouts..

Activity_main.xml

<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_main" 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_main" app:menu="@menu/activity_main_drawer" />

The only thing I succeed is to get the NavigationView.

NavigationView nv = (NavigationView) findViewById(R.id.nav_view);

Then i'm stuck here !

My nav_header_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
android:background="@drawable/icon_top"
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" android:orientation="vertical"
android:gravity="bottom">

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:src="@android:drawable/sym_def_app_icon" android:id="@+id/imageView" />

<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
    android:id="@+id/labelConnection"
    android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Se connecter..."
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"

    android:onClick="onClick"
    android:clickable="true" />

When I try to get nv.findViewById(R.id.labelConnection), I've a null Pointer Exception.

Why??

Edit : I've found a solution to inflate a headerLayout but then I've this result : https://gyazo.com/4450a3401a30d4000a3021a0a1cb6ea9

That's not good enough.. I don't know how to delete the first header ...

android_eng
  • 1,370
  • 3
  • 17
  • 40

1 Answers1

0

You need to do this

View headerView = nv.getHeaderView(0);
headerView.findViewById(R.id.labelConnection);

to remove the first header view do:

nv.removeHeaderView(nv.getHeaderView(0));

but make sure you check for duplicate headers otherwise it will throw indexOutOfBoundException.

android_eng
  • 1,370
  • 3
  • 17
  • 40