Reputation: 17132
I have a NavigationView
nested inside a DrawerLayout
.
activity_main.xml
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
.......
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
drawer_header.xml
<?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="150dp"
android:background="?attr/colorPrimaryDark"
android:gravity="bottom"
android:orientation="vertical"
android:padding="16dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<TextView
android:id="@+id/drawerHeaderTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/drawer_header_text"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
How can I access the drawer header's title in Java code ? I tried to:
access it directly using its id
(drawerHeaderTitle) but I get a NullPointerException
access it via the NavigationView.findViewById
: same error
How can I overcome this problem ?
Upvotes: 4
Views: 6064
Reputation: 1
Declaration
private NavigationView mNavigationView;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
mTextView = (TextView) mNavigationView.getHeaderView(0).findViewById(R.id.id_textView);
mTextView.setText("text user");
}
Upvotes: 0
Reputation: 141
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="Dashboard"/>
app:title="Dashboard" -> You can change your title here..
Upvotes: 1
Reputation: 3502
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View mHeaderView = navigationView.getHeaderView(0);
TextView companyNameTxt = (TextView) mHeaderView.findViewById(R.id.nav_company_name);
companyNameTxt.setText("Test Company Name, LLC");
Upvotes: 1
Reputation: 22212
If you compile with 'com.android.support:design:23.1.1' you can access your header in this way:
private NavigationView mNavigation;
private View mHeaderView;
private TextView mDrawerHeaderTitle;
mNavigation = (NavigationView) findViewById(R.id.navigation_view);
mHeaderView = mNavigation.getHeaderView(0);
mDrawerHeaderTitle = (TextView) mHeaderView.findViewById(R.id.drawerHeaderTitle);
More info about this: http://developer.android.com/intl/es/tools/support-library/index.html
Upvotes: 3
Reputation: 1388
Using this approach
mNavigationView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
mNavigationView.removeOnLayoutChangeListener( this );
TextView textView = (TextView) mNavigationView.findViewById(R.id.drawerHeaderTitle);
textView.setText("title");
}
});
Upvotes: 5