SandeepC SandeepC - 3 months ago 41
Android Question

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);`

Answer

u get navigation header view by navigationView.getHeaderView(0) just try below code it works!

 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);