Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text to view from drawer header layout in navigation drawer without inflating view

I want to set text to the navigation drawers headers text, I searched a lot but everyone does it by inflating a layout but for me it creates 2 headers so is there any way to set it without inflating the layout?

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

View header=navigationView. findViewById(R.id.header);
/*View        view=navigationView.inflateHeaderView(R.layout.nav_header_main);*/
name = (TextView)header.findViewById(R.id.username);
email = (TextView)header.findViewById(R.id.email);
name.setText(personName);
email.setText(personEmail);`
like image 976
SandeepC Avatar asked Nov 30 '15 13:11

SandeepC


People also ask

How do I customize my navigation drawer?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to res/layout/nav_header_main.

Which view is used to add the navigation drawer to an app?

Figure 3. An open drawer displaying a navigation menu. The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view.


1 Answers

You get navigation header view by navigationView.getHeaderView(0) , try code below

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);
/*View view=navigationView.inflateHeaderView(R.layout.nav_header_main);*/
name = (TextView)header.findViewById(R.id.username);
email = (TextView)header.findViewById(R.id.email);
name.setText(personName);
email.setText(personEmail);
like image 137
The Black Horse Avatar answered Oct 11 '22 07:10

The Black Horse