1

I'm using the latest android navigation drawer. But I have some problems with the header, I want to dynamically change it. How can I do that? Can I modify the textview in header layout that has been used for header in navigationview?

Obscure Geek
  • 749
  • 10
  • 22
huzain07
  • 1,051
  • 2
  • 8
  • 17
  • Possible duplicate of http://stackoverflow.com/questions/33560219/in-android-how-to-set-navigation-drawer-header-image-and-name-programmatically-i – Obscure Geek Mar 16 '16 at 15:50
  • see my answer [answer](http://stackoverflow.com/questions/34792887/android-navigationview-displays-under-navigationbar-and-cannot-click-how-to-so/34793614#34793614) – mehrdad khosravi Mar 16 '16 at 15:53

1 Answers1

2

Can i modify the textview in header layout that has been used for header in navigationview?

YES! You can!

I assume you are using a NavigationView, something like this:

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

Let's assume the nav_header_home is this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:id="@+id/mainText"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/subText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android.studio@android.com" />

</LinearLayout>

Now to change the mainText or the subText. All you have to do in your Activity is findViewById and modify whatever you want. For instance:

 protected void onCreate(Bundle savedInstanceState) {
    //// Your initiation code
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    View hView =  navigationView.getHeaderView(0);
    TextView mainText = (TextView)hView.findViewById(R.id.mainText);
    mainText.setText("Hello World!");
} 
Eric B.
  • 4,622
  • 2
  • 18
  • 33