Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to control views inside NavigationView header?

As the title says, I want to know if there is any way to control views inside the NavigationView header? (Except for adding or removing header.)

For example: In the header, I have a user avatar. By default, it displays a guest image, but after the user logs in, the real avatar will be showed.

How can this be accomplished?

like image 963
Lạng Hoàng Avatar asked Jul 06 '15 09:07

Lạng Hoàng


2 Answers

After updating your support library to version 23.1.1 or above,

You could do this -

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

or if you have multiple headers

navigationView.getHeaderCount()

Ref : https://code.google.com/p/android/issues/detail?id=190226#c31

like image 181
RmK Avatar answered Oct 30 '22 20:10

RmK


Since i can not accept a comment as an answer. So, i repost Moinkhan' answer here:

first create header XML like lay_header.xml

<TextView
    android:id="@+id/tvThought"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

on your java file inflate this above header in a TextView. like

TextView headerView = (TextView) LayoutInflater.from(this).inflate(R.layout.lay_header, null);
headerView.setText("Your_thoght");

Now add it as a HeaderView

navView = (NavigationView) findViewById(R.id.navView);
navView.addHeaderView(headerView);

Thats it...

You can also check it out at: Customising NavigationView - Adding dynamic headerView, Android Support Design Library

Many thanks! And again, we need a function that allow user to accept a comment as answer!

like image 7
Lạng Hoàng Avatar answered Oct 30 '22 20:10

Lạng Hoàng