Android API 23 Change Navigation View Headerlayout Textview

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:

  1. Use findViewById(R.id.nav_title) to find the TextView
  2. use setText() to set a text to the TextView

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



Leave a reply



Submit