I have such a NavigationView

enter image description here

Here is the code:

 <?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_mainsecond" 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_mainsecond" app:menu="@menu/activity_therd_drawer" /> </android.support.v4.widget.DrawerLayout> 

and here's a header :

 <?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="160dp" 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"> <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="start" android:paddingTop="20dp" android:layout_above="@+id/tvName" android:src="@drawable/newlogo" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_above="@+id/textEmail" android:text="Current user name" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <TextView android:id="@+id/textEmail" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="android.studio@android.com" /> </RelativeLayout> 

I need in this header in the field for the name user to put his name, and in the field for email to put respectively email

I understand that everything is standard and you need to find by id view with which you want to work and install TextView in it.

But when I find the view and put the text , the error takes off that there is no such view yet. This is logical because NavigationView not yet open ...

So here is the question. How to track that NavigationView already open and only after that set text in view ?

If I find and set the text in onCreate()

 TextView tvName = (TextView) findViewById(R.id.tvName); TextView textEmail = (TextView) findViewById(R.id.textEmail); tvName.setText("test"); textEmail.setText("test"); 

I get this error in the string tvName.setText("test");

FATAL EXCEPTION: main Process: com.example.android.camera2basic, PID: 32152 java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.android.camera2basic / com.example.android.camera2basic.activities.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference

If you use this approach, then there is no error, but the text remains the same as by default.

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); RelativeLayout headerView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.nav_header_mainsecond, null); TextView tvName = (TextView) headerView.findViewById(R.id.tvName); TextView textEmail = (TextView) headerView.findViewById(R.id.textEmail); tvName.setText("name"); textEmail.setText("email"); 
  • You take this data from the database? if so, do you have a check (function) that the user has logged in? - iFr0z
  • It would be desirable the text of the error and the code where it falls - Android Android
  • @ iFr0z I take the data from SharedPref , well, like a check ... I check if the data is there means you need to install it - Aleksey Timoshchenko
  • @AndroidAndroid added - Aleksey Timoshchenko
  • Well, the mistake is due to the fact that you are trying to pull the views out of the second markup, and you have xml-ka installed then the first one =) - Android Android

1 answer 1

You need:

  1. Get NavigationView

NavigationView nv = ...;

  1. Get his Header markup

View header = nv.getHeaderView(0);

  1. Find the fields you need and do something with them.

TextView tv = (TextView)header.findViewById(...);

  • Exactly !!)) Cool)) From where do you know this? It all worked! Thank! - Aleksey Timoshchenko
  • @AlekseyTimoshchenko, well ... I did it once ... And the memory is not very bad, that's all) - Yuriy SPb
  • one
    @AlekseyTimoshchenko, as a rule, everything has long been known and carefully recorded - you just need to read and select the right one :) - pavlofff