Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView from NavigationView header returning null

Tags:

android

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>
like image 382
Shaik MD Ashiq Avatar asked Nov 05 '15 08:11

Shaik MD Ashiq


2 Answers

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);
like image 198
Marco Aurelio Avatar answered Nov 03 '22 00:11

Marco Aurelio


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();

like image 27
user9884414 Avatar answered Nov 03 '22 01:11

user9884414