0

I have a navigation menu as you see in the image. I am able to get the user details from the database and am able to display them on other textviews in the app. But when I try to create the TextView object for the TextViews in the Navigation Menu, I see in the debug values that the reference it is getting is "null".

Navigation Menu

 TextView firstName = (TextView) findViewById(R.id.textview_firstName); firstName.setText("test");

I am just trying to set it to "test" for testing purpose.

activity_navigation_main.xml (its not activity. just named it that way)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="fill_parent"
    android:layout_height="200dp"
    android:background="@drawable/side_nav_bar"
    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"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    tools:context="com.xtremustechnologies.chotu.HomeActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="66dp"
        android:layout_height="wrap_content"
        android:paddingTop="20dp"
        app:srcCompat="@android:drawable/sym_def_app_icon"
        android:layout_weight="0.17" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textview_lastName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="LAST NAME"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:textAllCaps="true"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textSize="20dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:text=", "/>

        <TextView
            android:id="@+id/textview_firstName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:hint="First Name"
            android:paddingTop="@dimen/nav_header_vertical_spacing"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textSize="20dp" />
    </LinearLayout>

    <TextView
        android:id="@+id/textview_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/email" />

</LinearLayout>

Image - activity_navigation_mail.xml

Please suggest.!

1 Answers1

0

You cannot access the views of navigation drawer directly, you need to access them through navigation view reference. Check this:

//Initializing NavigationView
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    View header = navigationView.getHeaderView(0);

    TextView firstName= (TextView) header.findViewById(R.id.textview_firstName);

Hope this will work for you!

Zohaib Hassan
  • 984
  • 2
  • 7
  • 11