How to instantiate views of another layout?

Asked

Viewed 169 times

0

I have the layout main and the code that controls it, this layout has a Navigation Drawer (the "slider" on the left) and left the layout of the Main Header (the top of the Navigation Drawer where the user information is) in another file. I tried to instantiate these views by FindViewById of MainActivity thinking it would work because the main_header is included in the layout of main, but it doesn’t work, I debugged the code and saw that the variables that should have that view of the main_header were receiving null, as if it were not possible to access them from there, as I can instantiate them?

(I’m doing the project in Xamarin Android, but so far the operation has seemed the same to me compared to Java, then if you know how to answer in Java help as well.)

1 answer

0


If anyone needs an answer in the future, also: the NavigationView (where you define the main header) has a method called getHeaderView(), after instantiating the NavigationView you "save" his header in another view and use it to instantiate.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.