38

I am testing the Navigation Drawer sample project in android and i have a problem setting the text in navigation view profile header.

This is my code:

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    TextView text = (TextView) findViewById(R.id.textView);
    texto.setText("HELLO");
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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" />

</android.support.v4.widget.DrawerLayout>

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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/side_nav_bar"
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:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="android.studio@android.com" android:id="@+id/textView" />

</LinearLayout>

When i try to set the text of the textview i always get this error and it only happens with api level 23:

java.lang.NullPointerException: Attempt to invoke virtual method   'void android.widget.TextView.setText(java.lang.CharSequence)' on a null   object reference

How can i change the Textview text of the nav_header_main.xml from the main activity??

Thanks in advance

user3065901
  • 4,678
  • 11
  • 30
  • 52
  • Having same issue. maybe a problem with library? – BigDX Oct 18 '15 at 17:21
  • 3
    I have this problem too - introduced with the dependencies in com.android.support:* 23.1.0 . Reverting to 23.0.1 fixed it, so I'll stay with that until it's addressed. – Carl Whalley Oct 18 '15 at 19:52
  • 4
    Same problem here... I think its a bug in new `com.android.support:* 23.1.0`.... – Vipin Kumar Oct 20 '15 at 07:12
  • 1
    This indeed an issue with the design support library 23.1.0. Because the underlying implementation for the NavigationView is now a RecyclerView, the header layout is not inflated at runtime. See link here: http://stackoverflow.com/questions/33364276/getting-error-in-existing-code-after-updating-support-repository-to-23-1-0/33365230#33365230 – HappyKatz Nov 04 '15 at 08:14
  • 1
    On another note: this sort of thing makes me shudder for the future of the self-driving car! – HappyKatz Nov 04 '15 at 08:15

7 Answers7

57

Now with the 23.1.1 release of the design support library, you can use

View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);
German
  • 784
  • 5
  • 4
45

I had the same issue and was able to avoid it with this code:

    View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
    navigationView.addHeaderView(header);
    TextView text = (TextView) header.findViewById(R.id.textView);
    text.setText("HELLO");
cjds
  • 8,268
  • 10
  • 49
  • 84
orium
  • 3,743
  • 2
  • 24
  • 27
  • Great answer. Works perfect – BigDX Oct 19 '15 at 01:58
  • if this is the only solution, why do we have a xmls? isnt there a work around to change via xml itself? – bks4line Nov 13 '15 at 05:50
  • This works fine, remove ` app:headerLayout="@layout/nav_header_main"` to avoid duplicate headers. – Gpak Jun 01 '16 at 08:58
  • At `LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);`, AndroidStudio complains with `Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)`. Is there any way to fix this? – Alexander Sep 27 '16 at 03:58
  • I still get the same crash when I deploy a debug apk, however, every app launch thereafter works properly... – DoruChidean Feb 16 '17 at 09:29
  • @orium Thanks a lot for the great solution .. Also, View headerView = navigationView.getHeaderView(0); helped me.. – Rakesh L Sep 11 '17 at 18:18
7

Orium answer works and this will work as well.

View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
    TextView text = (TextView) header.findViewById(R.id.textView);
    texto.setText("HELLO");
1

this solved the problem for me

    View headerView = navigationView.inflateHeaderView(R.layout.header_view);

    txt_fullname = (TextView) headerView.findViewById(R.id.txt_fullname);
    txt_email = (TextView) headerView.findViewById(R.id.txt_email);
Kamlesh
  • 421
  • 5
  • 9
0
View headerView=navigationView.getChildAt(0);
TextView tvName=(TextView)hView.findViewById(R.id.name);
TextView tvEmail=(TextView)hView.findViewById(R.id.email);

    tvName.setText(data);
    tvEmail.setText(data);
Mahadev Dalavi
  • 173
  • 1
  • 7
  • This solution works. I also tried: `textView = navigationView.getHeaderView(0).findViewById(R.id.textview);` and `View header = LayoutInflater.from(this).inflate(R.layout.menu_header, null); navigationView.addHeaderView(header); textView = header.findViewById(R.id.textview);` but unfortunately without success. I don't know why. – Anad May 09 '18 at 11:01
0

Or if you use this attribute:

<android.support.desibn.widget.NavigationView>
    ...
    app:headerLayout="@layout/your_header_layout"/>

You can:

View header = mNavigationView.getHeaderView(0);
TextView title = (TextView) header.findViewById(R.id.your_title_id);
//Or something else
TarikW
  • 359
  • 4
  • 12
0

The above answer is right but in kotlin, you need to use findViewById<View>

   val navigationHeader = navigation_view.getHeaderView(0)
        val navigationTitle = navigationHeader.findViewById<View>(R.id.txt_navigation_title) as TextView
        navigationTitle.text = "This is your header"
Md Imran Choudhury
  • 9,343
  • 4
  • 62
  • 60