In android how to set navigation drawer header image and name programmatically in class file? In android how to set navigation drawer header image and name programmatically in class file? android android

In android how to set navigation drawer header image and name programmatically in class file?


NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);View hView =  navigationView.getHeaderView(0);TextView nav_user = (TextView)hView.findViewById(R.id.nav_name);nav_user.setText(user);

hope this help!


As mentioned in the bug 190226, Since version 23.1.0 getting header layout view with:navigationView.findViewById(R.id.navigation_header_text) no longer works.

A workaround is to inflate the headerview programatically and find view by ID from the inflated header view.

For example:

View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);headerView.findViewById(R.id.navigation_header_text);

Ideally there should be a method getHeaderView() but it has already been proposed, let's see and wait for it to be released in the feature release of design support library.


don't add header in xml add using code by inflating layout

View hView =  navigationView.inflateHeaderView(R.layout.nav_header_main);ImageView imgvw = (ImageView)hView.findViewById(R.id.imageView);TextView tv = (TextView)hView.findViewById(R.id.textview);imgvw .setImageResource();tv.settext("new text");