Search code examples
androidtextviewwidget

how to control views inside navigationview header


i followed a tutorial at http://www.android4devs.com/2015/06/navigation-view-material-design-support.html and have implemented it in my project but i wanat to change the text in the header view dynamically as a user logs in, but my reference to the textview always returns as null, i've tried everything.

heres my header.xml code

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="220dp"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/background_material"
android:orientation="vertical"
android:id="@+id/header_relative_layout"
tools:context=".NavMainActivity">

<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="76dp"
    android:layout_height="76dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="24dp"
    android:layout_marginStart="24dp"
    android:src="@drawable/profile"
    app:border_color="#FF000000" />

<TextView
    android:id="@+id/username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/schoolname"
    android:layout_alignLeft="@+id/profile_image"
    android:layout_alignStart="@+id/profile_image"
    android:gravity="left"
    android:paddingBottom="4dp"
    android:text="Staff Name"
    android:textColor="#FFF"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/schoolname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/username"
    android:layout_alignParentBottom="true"
    android:layout_alignStart="@+id/username"
    android:layout_marginBottom="8dp"
    android:gravity="left"
    android:text="Secondary School"
    android:textColor="#fff"
    android:textSize="18sp" />

</RelativeLayout>

and heres my nav_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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".NavMainActivity">

<LinearLayout
    android:id="@+id/main_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar" />

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

    </FrameLayout>

</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"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>

heres my activity

setContentView(R.layout.nav_activity_main);
RelativeLayout headerLayout = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.header, null);
TextView staff = (TextView) headerLayout.findViewById(R.id.username);
staff.setText("it worked");

ive tried all solutions none seems to work, pls what am i doing wrong.


Solution

  • In order to interact with elements in the header of the navigation drawer, you first have to get its layout like this:

    // Initializing NavigationView
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    
    if (navigationView != null) {
        // Get the header layout
        headerLayout = navigationView.getHeaderView(0);
    }
    

    afterwards you can manipulate views by doing the following:

    TextView staff = (TextView) headerLayout.findViewById(R.id.username);
    staff.setText("it worked");