Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the Navigation Drawer Header items from Fragment?

I need to access my header items from a Fragment but I can't find a way to do it. I've seen:

View headerLayout = navigationView.getHeaderView(0);

However, I am not working on the NavigationDrawerActivity itself. I have to access it inside a Fragment alone. Is there a way for me to achieve that?

like image 714
kiwiLime Avatar asked Dec 31 '22 18:12

kiwiLime


1 Answers

It's easy! getHeaderView() can be called from an Activity. But, as you are in a Fragment, you just need to getActivity from the Fragment and then call getHeaderView()...

SOLUTION:

In your Fragment:

NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);

View headerView = navigationView.getHeaderView(0);

I hope it helps! Let me know if it didn't work!

like image 181
Farhan Ibn Wahid Avatar answered Jan 13 '23 14:01

Farhan Ibn Wahid