6

I am using NavigationView which has a header layout, that header layout contains a textview which I am trying to access in my Activity and it returns null.

Tried spending a lot of time on it, no hope.

My Activity

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    RelativeLayout headerLL;
    ImageView userIV;
    TextView usernameTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL);
        userIV = (ImageView) findViewById(R.id.userDPIV);
        usernameTV = (TextView) findViewById(R.id.usernameTV);
        //getting error here
        usernameTV.setText("Ashiq");



        initializeDrawer();

    }
}

activity_mail.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <include layout="@layout/include_list_viewpager"/>

        <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/nav_header"
            app:menu="@menu/drawer_view"
            />

    </android.support.v4.widget.DrawerLayout>

nav_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="192dp"
        xmlns:tools="http://schemas.android.com/tools"
        android:background="?attr/colorPrimaryDark"
        android:padding="16dp"
        android:id="@+id/navHeaderLL"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:id="@+id/userDPIV"
            android:elevation="4dp"
            android:layout_above="@+id/usernameTV"
            android:layout_marginBottom="10dp"
            tools:src="@drawable/ic_menu"
            />

        <TextView
            android:id="@+id/usernameTV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/TextViewAppearance.UserName"
            tools:text="Username"
        android:layout_alignParentBottom="true"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

</RelativeLayout>
Bullionist
  • 2,070
  • 3
  • 25
  • 42
  • 2
    please check this: http://stackoverflow.com/questions/31241683/are-there-any-way-to-control-view-insde-navigationview-header – denis_lor Nov 05 '15 at 08:58
  • @denis_lor: please look at this https://github.com/antoniolg/MaterializeYourApp/blob/master/app/src/main/java/com/antonioleiva/materializeyourapp/MainActivity.java – Bullionist Nov 05 '15 at 09:02

3 Answers3

36

After initialize the NavigationView you catch the header inflated with navigationView.getHeaderView(0) and use the findViewById to get the View

navigationView = (NavigationView) findViewById(R.id.nav_view);
(TextView) navigationView.getHeaderView(0).findViewById(R.id.viewName);
Marco Aurelio
  • 505
  • 1
  • 4
  • 5
1

public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
RelativeLayout headerLL;
ImageView userIV;
TextView usernameTV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL); // line to change
    userIV = (ImageView) findViewById(R.id.userDPIV);
    usernameTV = (TextView) findViewById(R.id.usernameTV);
    //getting error here
    usernameTV.setText("Ashiq");



    initializeDrawer();

}

}

// for the "line to change", you have to getRootView by doing

headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL).getRootView();

Jayhymn
  • 76
  • 4
0

Do this.

  1. You should Initialize the NavigationView (next of setcontentview)

2.After, initialize the Navigation View element

Change the code as below

 public class MainActivity extends AppCompatActivity {
    private DrawerLayout mDrawerLayout;
    RelativeLayout headerLL;
    ImageView userIV;
    TextView usernameTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 1
        initializeDrawer(); 

        //2

        headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL);
        userIV = (ImageView) findViewById(R.id.userDPIV);
        usernameTV = (TextView) findViewById(R.id.usernameTV);
        //getting error here
        usernameTV.setText("Ashiq");


    }
}

Hope this helps.

Happy Coding :)

Venkatesh Selvam
  • 1,382
  • 2
  • 15
  • 28
  • My answer is correct one. How you get the NV elements value without Initialize the Navigation drawer. ? @Shaik – Venkatesh Selvam Nov 05 '15 at 09:25
  • Take a look at [here](http://developer.android.com/training/implementing-navigation/nav-drawer.html) . Dont give a Downvote who answers for questions without analyse their answers. if you continue like this. No one will come & give the answers for you. – Venkatesh Selvam Nov 05 '15 at 09:37
  • So, you give the wrong program (these lines are yours "First of all do you know what I do inside initializeDrawer() method "). You should learn "How to write in Stackoverflow questions". At First you should learn Basics from "Developers.android.com" . Lol. i didn't delete my answers, if any developer seen this Q&A. they know whose answer is correct... – Venkatesh Selvam Nov 05 '15 at 09:57
  • Ha ha .. Great escape. – Venkatesh Selvam Nov 05 '15 at 10:03
  • You only give useless information. First you should learn from the Android developers official page. http://developer.android.com/training/implementing-navigation/nav-drawer.html.. Good Bye. – Venkatesh Selvam Nov 05 '15 at 10:06