2

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.

N Sharma
  • 33,489
  • 95
  • 256
  • 444

3 Answers3

2

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");
JBA
  • 2,889
  • 1
  • 21
  • 38
1

Need to try code like below to get View from Navigationview.

For Android support 23.1.0 can be to use a addOnLayoutChangeListener. Somenthing like:

headerNavigationView.addOnLayoutChangeListener( new View.OnLayoutChangeListener()
{
    @Override
    public void onLayoutChange( ... )
    {
        navigationView.removeOnLayoutChangeListener( this );

        View header = navigationView.getHeaderView(0)
        TextView tvUsername = (TextView) header.findViewById(R.id.R.id.username);
        tvUsername.setText(userData.getName());
    }
} );

Check this answer.

I hope it's helps you.

Community
  • 1
  • 1
pRaNaY
  • 24,642
  • 24
  • 96
  • 146
  • I am already doing that.. my `headerNavigationView` is the header view.. my question is not how to get view from navigation view..I am getting that but value is not being refreshed – N Sharma Jan 26 '16 at 18:07
  • it is not related to my question.. started bounty – N Sharma Feb 06 '16 at 17:52
0

The implementation of NavigationView changed recently. You can no longer reach into NavigationView to find the header of a particular id. Instead you should use addHeaderView to add a view of your own createion or inflateHeaderView, which returns the view you're looking for.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441