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:
MainAc
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);
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");
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");
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
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);
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"