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
Easiest Way to Convert a List to a Set in Java
Configuring Log4J Loggers Programmatically
How to Capitalize the First Letter of a String in Java
"Java Dateformat Is Not Threadsafe" What Does This Leads To
Jackson Enum Serializing and Deserializer
How Do Hashtables Deal with Collisions
Getting Spring Application Context
Should I Use Java Date and Time Classes or Go with a 3Rd Party Library Like Joda Time
Differencebetween ? and Object in Java Generics
How to Change Webservice Url Endpoint
Getting Java Gui to Open a Webpage in Web Browser
How to Populate a Drop Down with a List Using Thymeleaf and Spring
Getting Jsoup to Support Dynamically Generated HTML by JavaScript
Remove Padding/Margin from Javafx Label
How to Use Jsp/Jstl to Generate Dynamic CSS/JavaScript Files