Android Api 23 Change Navigation View headerLayout textview
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");
How to change text of a TextView in navigation drawer header?
Use function getHeaderView on your navigationView:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
TextView navUsername = (TextView) headerView.findViewById(R.id.navUsername);
navUsername.setText("Your Text Here");
Change Design Support Navigation View Header Title Programmatically
In your Java code:
- Use
findViewById(R.id.nav_title)
to find theTextView
- use
setText()
to set a text to theTextView
For example, in your MainActivity:
TextView navTitle = (TextView) findViewById(R.id.nav_title);
navTitle.setText("This is a changed string.");
Change the NavigationView's header title
Using this approach
mNavigationView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
mNavigationView.removeOnLayoutChangeListener( this );
TextView textView = (TextView) mNavigationView.findViewById(R.id.drawerHeaderTitle);
textView.setText("title");
}
});
how to change Navigation header widgets value dynamically?
You can get the header via NavigationView
:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
ImageView drawerImage = (ImageView) headerView.findViewById(R.id.drawer_image);
TextView drawerUsername = (TextView) headerView.findViewById(R.id.drawer_username);
TextView drawerAccount = (TextView) headerView.findViewById(R.id.drawer_account);
drawerImage.setImageDrawable(R.drawable.ic_user);
drawerUsername.setText("User");
drawerAccount.setText("user@gmail.com");
NavigationView how to handle dynamic header content
You can do that in code by inflating custom layout and set it's header for navigation view.
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
View nav_header = LayoutInflater.from(this).inflate(R.layout.nav_header, null);
((TextView) nav_header.findViewById(R.id.name)).setText("UserName");
navigationView.addHeaderView(nav_header);
You don't need to set app:headerLayout
in xml.
Related Topics
Fragment Standard Transition Not Animating
Writing Some Characters Like '<' in an Xml File
What Is Better: @Suppresslint or @Targetapi
Android Intercept Recent Apps Button
Android Viewmodel Has No Zero Argument Constructor
Adb Shell Su Works But Adb Root Does Not
How to Replace The Activity's Fragment from The Fragment Itself
View Pager + Imageview +Pinch Zoom + Rotation
Make Android Webview Not Store Cookies or Passwords
Adding Spinner to Actionbar (Not Navigation
Disable Webview Touch Events in Android
Android Studio (Not Installed), When Run Flutter Doctor While Android Studio Installed on Machine
Programmatically Lock into Portrait Mode for Certain Operations
How to Save Data from Camera to Disk Using Mediastore on Android