27

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?

Drenmi
  • 8,492
  • 4
  • 42
  • 51
Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32
  • Its up to you what you are inflating in header view. You can take `ImageView` inside `LinearLayout` and change ImageDrawable dynamically . – Moinkhan Jul 06 '15 at 09:04
  • Could you show more details, ex my header just with textview inside FrameLayout. – Lạng Hoàng Jul 06 '15 at 09:15
  • 2
    refer my this answer .. http://stackoverflow.com/a/30660069/3544839 ... – Moinkhan Jul 06 '15 at 09:17
  • in above link i change textview so you just take imageview and change drawable instead of setText of textview.. – Moinkhan Jul 06 '15 at 09:18
  • ah, i see your answer before. But as i mentioned (except for removing/adding header), i'm finding another way to do that. Maybe a way to get access to the header through the navigationview. – Lạng Hoàng Jul 06 '15 at 09:23
  • 1
    So for that you can use inflateHeaderView(int resID) of NavigationView. Which will give you View object in return so that you can access the full layout using view object – Moinkhan Jul 06 '15 at 09:31
  • thanks for your help :) – Lạng Hoàng Jul 24 '15 at 09:15
  • 1
    Until now, the best answer is Moikhan at http://stackoverflow.com/questions/30621047/customising-navigationview-adding-dynamic-headerview-android-support-design-l/30660069#30660069 – Lạng Hoàng Jul 24 '15 at 09:18
  • 1
    you can vote up there bro ...lolzzz – Moinkhan Jul 24 '15 at 09:36
  • please check this answer for correct solution http://stackoverflow.com/a/33365230/3792198 – Thiago Nov 05 '15 at 06:48

4 Answers4

59

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

RmK
  • 1,408
  • 15
  • 28
7

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!

Community
  • 1
  • 1
Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32
1

The other way to make this is using the method "inflateHeaderView()" of NavigationView object, like below (After using "setContentView()"):

View headerLayout = navigationView.inflateHeaderView(R.layout.yours_nav_header_layout);

After this point you can use "headerLayout" to access custom views in your header layout, like below:

View cutomView = (TextView) headerLayout.findViewById(R.id.lay_sign_in);

In my case i'm using ButterKnife framework to inject View "navigationView" like this:

@Bind(R.id.nav_view) NavigationView navigationView;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }
GFPF
  • 959
  • 14
  • 19
  • 1
    This method is fine as long as the header is not already defined in the xml of navigationview . if so, calling inflateheaderview adds an extra header in the side drawer. – RmK Nov 17 '15 at 08:57
0

Simply try find your view in your activity:

tvUserName = (TextView) findViewById(R.id.tv_username);

It works for me. My header layout is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:background="?attr/colorPrimaryDark"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical"
    android:gravity="bottom">

    <TextView
        android:id="@+id/tv_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</LinearLayout>

My NavigationView in Activity Layout is:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/menu_drawer"/>

And BTW, I came across problem when inflating LinearLayout, where layout params are not provided:

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

Hope it helps.

Jiawei Dai
  • 604
  • 7
  • 6
  • You don't seem to reference the actuall TextView's ID. How would you differentiate or identify the actual TextView you want when you have more than one TextViews in the header layout? I presume you would inflate it as a View then from the view you can get the TextView by id? – Nerudo Oct 29 '15 at 01:22
  • @Nerudo, well, it 's there in Header layout: android:id="@+id/tv_username". and reference it by ID findViewById(R.id.tv_username); – Jiawei Dai Oct 30 '15 at 04:06