1

I have a TextView inside my NavigationView tag. But I can't get it with findViewById method:

<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:itemIconTint="@color/drawer_item"
    app:itemTextColor="@color/drawer_item"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/version_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginStart="20dp" />

    </RelativeLayout>

</android.support.design.widget.NavigationView>

I have tried following ways but I always get null.

findViewById(R.id.version_name); //null
navigationView.findViewById(R.id.version_name); //null
navigationView.getRootView.findViewById(R.id.version_name); //null
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
  • 1
    http://stackoverflow.com/questions/33161345/android-support-v23-1-0-update-breaks-navigationview-get-find-header/33163288#33163288 – Gabriele Mariotti Feb 12 '16 at 13:22
  • Possible duplicate of http://stackoverflow.com/questions/33199764/android-api-23-change-navigation-view-headerlayout-textview – Jay Rathod Feb 12 '16 at 13:22

2 Answers2

3

What I was trying to do is add a foother to my navigation drawer to show app version. Since app version is a dynamic data, adding a static text view to below navigation view and wrap them with an outer layout also could't work. The correct approch to solve the problem is described below link:

NavigationView with Foother

Main idea is puting two NavigationView in a NavigationView: One for normal navigation items and second for foother items.

<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/layout_dashboard"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <!-- Activity content goes here -->

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_drawer"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            app:menu="@menu/menu_navigation_drawer" />

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_drawer_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:menu="@menu/menu_navigation_drawer_bottom" />

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
0

I do have the same problem..

View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
navigationView.addHeaderView(header);
TextView text = (TextView) header.findViewById(R.id.textView);
texto.setText("Your text");
Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56