How to Set Text to View from Drawer Header Layout in Navigation Drawer Without Inflating View

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

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

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

How to get view from drawer header layout with binding in activity?

Updated solution (13/11/2015)

Solution: Update your Design Support Library to 23.1.1:

Changes for Design Support library 23.1.1:

  • Added the getHeaderView method to the NavigationView class.
  • Fixed a transparent background issue for a FloatingActionButton object on devices running Android 4.0 (API level 15) and lower. (Issue 183315)

See https://developer.android.com/tools/support-library/index.html for more info



Original solution

I don't know why there is no method which provides header view
attached programmatically.

Instead, here's two solutions:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = navigationView.inflateHeaderView(R.layout.header_layout)
ImageView iv = (ImageView)headerview.findViewById(R.id.your_image_view)

Or:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, navigationView, false);
navigationView.addHeaderView(headerView);

ImageView iv = (ImageView) headerView.findViewById(R.id.yourImageView)

android - How to access the Navigation Drawer Header Items?

You need to inflate the header view as it is not inflated automatically .

View header = mNavigationView.getHeaderView(0);
mNameTextView = (TextView) header.findViewById(R.id.nameTextView);
mNameTextView.setText("XYZ");

Changed name and email on navigation drawer display profil like playstore

You have to get the header view first, then after that you will need to get the child views that you are interested in, before you can set the text e.g.

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//... other onCreate setup

NavigationView mNavigationView = findViewById(R.id.nav_view);
View headerView = mNavigationView.getHeaderView(0);
// get user name and email textViews
TextView userName = headerView.findViewById(R.id.text_user_name);
TextView userEmail = headerView.findViewById(R.id.text_email_address);
// set user name and email
userName.setText("username");
userEmail.setText("email.user@domain.com");

}

Can't set the textview in the navigation drawer using navigation view in main activity. Xamarin Android

You use LayoutInflator to inflate a view from nav_header, which means you created a total new View from nav_header.xml and changed it's sub textview's text and didn't use this new view in your activity. Thus the text of your original activity won't change.

solution:
Modify your activity codes like this:

protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.main);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
{
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeButtonEnabled(true);
}
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout); SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
//LayoutInflater inflater = (LayoutInflater)GetSystemService(Context.LayoutInflaterService);
//View view = inflater.Inflate(Resource.Layout.nav_header, null);
//UserNameTxt = view.FindViewById<TextView>(Resource.Id.UserNameTxt);
//UserNameTxt.Text = "Yousuf";
LinearLayout header=(LinearLayout)navigationView.GetHeaderView(0);
UserNameTxt = header.FindViewById<TextView>(Resource.Id.UserNameTxt);
UserNameTxt.Text = "Yousuf";
...


Related Topics



Leave a reply



Submit