-1

I have a NavigationView in a Drawer Layout which contains a HeaderView followed by a Menu (as given in AndroidStudio's Navigation Drawer Activity's template). My Header contains an Image and couple of TextViews in a LinearLayout. I want to edit of one of the TextViews in the header from SharedPreferences. I use following function to do so

public void navUpdate()
{
        navigationView=(NavigationView)findViewById(R.id.nav_view);
        linearLayout=(LinearLayout) LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
        textView_nname=(TextView)linearLayout.findViewById(R.id.textView_nname);


        String name=sharedPreferences.getString("name","Your Name");
        textView_nname.setText(name);

        navigationView.removeHeaderView(linearLayout);
        navigationView.addHeaderView(linearLayout);
}

The problem is , though I've added the line

navigationView.removeHeaderView(linearLayout);

my previous headerview is still visible above the new one. And previous one is not updated. I read about the method navigationview.getHeaderAt() in many answers but believe me, that method is not available.

Here's how it looks

Is there any solution for this problem, if not is there any other way to do so?

fanatic
  • 7
  • 6
  • "navigationview.getHeaderAt()...that method is not available." - I believe you want the `getHeaderView(int index)` method. Also, if you just want to change the text in a header's TextView, you don't need to completely replace the header. Use the aforementioned method to get the header, and the `findViewById()` method to access the TextView you want to change. – Mike M. Jan 12 '16 at 02:09
  • @MikeM.- I'm sorry , my bad, I meant navigationView.getHeaderView() only. But you might wanna take look at http://i66.tinypic.com/sm3j21.png – fanatic Jan 12 '16 at 11:48
  • Make sure you're using the most recent library version. That method was just added about [three months ago](https://android.googlesource.com/platform/frameworks/support.git/+/cdccc4c6d5f1e40610ce4df4afeb1f99cbdb63e8%5E%21/#F2). – Mike M. Jan 12 '16 at 12:00
  • 1
    thank u so much. Its done. – fanatic Jan 13 '16 at 07:27

1 Answers1

1

in order for you to get the Header from the NavigationView you'll have to call getHeaderView(int index) for instance:

View headerView = navigationView.getHeaderView(0);
textView_nname = (TextView)headerView.findViewById(R.id.textView_nname);
// set whatever you like on the textView.
Kosh
  • 6,140
  • 3
  • 36
  • 67