Search code examples
androidnavigationviewandroid-navigationview

How to refresh the headview content of navigationview in android


Hello I am using NavigationView in one the demo project. I have header layout of NavigationView. I am getting data of header layout at the runtime and once I get data I am setting values of views but data is not reflected or refreshed.

((TextView)headerNavigationView.findViewById(R.id.username)).setText(userData.getName());

What is the correct way to refresh the navigationview at runtime once we get a data from web service.


Solution

  • I faced the same issue: it's not as much an issue with your webservice etc. than a bug in Android with the NavigationView Header. The workaround is to inflate the header programmatically like below... Let's assume you have a TextView called "version" in the header :

    NavigationView mNavigationView = (NavigationView) findViewById(R.id.navigation_view);    
    View headerLayout = mNavigationView.inflateHeaderView(R.layout.my_header_layout);
    // Now you can update the views in your header as you want :
    TextView version = (TextView) headerLayout.findViewById(R.id.version);
    version.setText("text in my header");