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>
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);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With