How to Change Text of a Textview in Navigation Drawer Header

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

Set Text in navigation drawer header

Make sure you initialize all View's properly before setting values. For example:

NavigationView navigationView = (NavigationView) findViewById(R.id.navbar);
View header = navigationView.getHeaderView(0);
TextView name = header.findViewById(R.id.name_text_view);
TextView email = header.findViewById(R.id.email_text_view);
TextView contact = header.findViewById(R.id.contact_text_view);

Then you can set your values.

name.setText(myname);
email.setText(myemail);
contact.setText(mycontact);

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

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

Android: Navigation Drawer Header Text View is not setting programmatically

You should change the code slightly as below.

  //        Set Drawer Header
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View headerView = LayoutInflater.from(this).inflate(R.layout.header, navigationView, false);
navigationView.addHeaderView(headerView);
TextView Name = (TextView) headerView.findViewById(R.id.name);
Title.setText("Myname");
TextView Number = (TextView) headerView.findViewById(R.id.number);
Email.setText("+1234567890");

Set Text from Fragment to Navigation Drawer in Activity

You do not want to call MyActivity myActivity = new MyActivity(); as it creates a new activity. Instead, you need to get reference to the activity that the fragment is in:

MyActivity myActivity = (MyActivity)getActivity();

There is also a documentation exactly on communicating between fragment and activity.

setText in Navigation Drawer

You could try this:

TextView name = (TextView) navigationView.getHeaderView(0).findViewById(R.id.username);


Related Topics



Leave a reply



Submit