Search code examples
androidnavigation-drawerandroid-navigationview

Setting NavigationView's Header Layout's Text


<android.support.design.widget.NavigationView
    android:id="@+id/leftNavigationView"
    android:background="@color/LIGHT_BEIGE_COLOR"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header_layout"
    app:menu="@menu/left_drawer"
    android:theme="@style/LeftNavigationViewStyle"/>

In my header_layout. I have an ImageView and TextView. I am wondering how do I set the value to the headerLayout TextView programmatically


Solution

  • You have to access to the header through the navigationView and then find your TextView by its Id.

        NavigationView navigationView = (NavigationView) findViewById(R.id. leftNavigationView);
        TextView headerTextView = (TextView) navigationView.getHeaderView(0).findViewById(R.id.header_text:view);