Duplicate value in NavigationView header after updating header text - android

After updating my header text and image in NavigationView header, I'm getting duplicate value in NavigationView header.
Here is my code part
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
boolean doubleBackToExitPressedOnce = false;
SQLiteHelper dbHelper;
String setName, setMail;
AlertDialog.Builder builder, builder_verify;
public static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbHelper = new SQLiteHelper(getApplicationContext());
builder_verify = new AlertDialog.Builder(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View hView = navigationView.inflateHeaderView(R.layout.nav_header_main);
ImageView iv = (ImageView) hView.findViewById(R.id.imageView);
TextView headerName = (TextView) hView.findViewById(R.id.txtName);
TextView headerMail = (TextView) hView.findViewById(R.id.textEmail);
iv.setImageResource(R.drawable.logo);
headerName.setText("TEST");
headerMail.setText("test#gmail.com");
}
}
In the above code i have added view to update my header text. Also i have added main XML code below
<?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_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<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_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Also added navigation header layout
nav_header_main.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="#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="#drawable/logo"/>
<TextView
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Sample"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textStyle="bold" />
<TextView
android:id="#+id/textEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Inc." /></LinearLayout>
Also i have added the screenshot for reference
Right now, I don't see any possibilities of changing the headerview. Any suggestions to avoid duplicate header text and image?
Many thanks!

Good question.
The fix is to instead of inflating header(it's already inflated!) by navigationView.inflateHeaderView(R.layout.nav_header_main); get it from NavigationView via getHeaderView(int index) method and then fill it.
Here's the code to run:
navigationView.setNavigationItemSelectedListener(this);
View hView = navigationView.getHeaderView(0);
ImageView iv = (ImageView) hView.findViewById(R.id.imageView);
TextView headerName = (TextView) hView.findViewById(R.id.txtName);
TextView headerMail = (TextView) hView.findViewById(R.id.txtEmail);
iv.setImageResource(R.drawable.logo);

In xml fie you have already set
app:headerLayout="#layout/nav_header_main"
for NavigationView and again in java class you'r inflating your view with
View hView = navigationView.inflateHeaderView(R.layout.nav_header_main);
So there is a duplicate values in NavigationView.

I have a easy solution for this issue and it is worked for me.
Before adding header views we must remove current header views from navigation drawer. This is the code snippet.
if (navigationView != null) {
for (int i = 0; i < navigationView.getHeaderCount(); i++) {
if (navigationView.getHeaderView(i) != null)
navigationView.getHeaderView(i).setVisibility(View.GONE);
}
addHeaderLayout(navigationView);
}

Related

How to use Butterknife in Combination with a navigation header?

I bind some Views with Butterknife but it doesn't work somehow.
The Exception i got is
java.lang.IllegalStateException: Required view 'full_name_nav'
which basically says that he can't find the view. So i think i am binding at the wrong position, but i don't know where i should do it correctly. I am including the Navigation Header Layout in the NavigationView and binding these views in my oncreate method.
My Code:
XML Layout of the Activity:
<?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_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_main" <!-- Including the Navigation Header here-->
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
XML Layout of the Navigation Header
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="#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"
app:srcCompat="#android:drawable/sym_def_app_icon" />
<TextView
android:id="#+id/user_name_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/full_name_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/motorcycle_model_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
My Main Activity where i actually bind the views:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#BindView(R.id.full_name_nav)
TextView navigationName;
#BindView(R.id.motorcycle_model_nav)
TextView navigaitonMotorcycle;
#BindView(R.id.user_name_nav)
TextView navigationUsername;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = this.getSharedPreferences(
"package", Context.MODE_PRIVATE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ButterKnife.bind(this); // Calling ButterKnife here
setNavInfo();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
I implemented the Viewbinding in the OnCreateOptionsMenu, now it works.

findViewById returns null in AppCompatActivity

I tried to change the Bitmap of an ImageView, but the code keeps returning a null Object.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
// Drawer Options
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// Drawer Header
ImageView img = (ImageView) findViewById(R.id.header_imageView);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.marius_cropped_48x48);
RoundedBitmap profile_pic = new RoundedBitmap(bm);
if (img != null) {
img.setImageBitmap(profile_pic.getRoundedBitmap());
Log.d(TAG, "ImageView OK");
} else {
Log.d(TAG, "ImageView null");
}
}
Here "img" is always null. I tried to change it to this but no luck.
ImageView img = (ImageView) drawer.findViewById(R.id.header_imageView);
or
LinearLayout header = (LinearLayout) drawer.findViewById(R.id.header_linearLayout);
ImageView img = (ImageView) header.findViewById(R.id.header_imageView);
but no luck. Here are my xml files.
activity_drawer.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_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_drawer"
app:menu="#menu/activity_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>
And the nav_header_drawer.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/header_linearLayout"
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/header_imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing" />
<TextView
android:id="#+id/header_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="text"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/header_company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="as" />
</LinearLayout>
To successfuly look for widget in Navigation Drawer's header, you need to get the correct container view first by calling getHeaderView() on it:
View header = ((NavigationView)findViewById(R.id.nav_view)).getHeaderView(0);
and then having header pointing to the right ViewGroup can call findViewById() on it for your target widget as usual:
ImageView iv = ((ImageView) header.findViewById(R.id.header_imageView));
Change following line from
ImageView img = (ImageView) drawer.findViewById(R.id.header_imageView);
to
ImageView img = (ImageView) navigationView.findViewById(R.id.header_imageView);
As header_imageView is the part of navigation view not drawer.

android null pointer exception on text view [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
In my simple app i have a textView on a NavigationView and i have this exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setVisibility(int)' on a null object reference. I don't understand because it tells me that invoke method on a null object. Code:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, ReadRssListener {
RecyclerView recyclerView;
private PrefUtil prefUtil;
private ImageView profileImgView;
private TextView info;
AccessTokenTracker accessTokenTracker;
LoginButton loginButton;
SwipeRefreshLayout refreshLayout;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();
prefUtil = new PrefUtil(this);
profileImgView = (ImageView) findViewById(R.id.thumb_img);
info = (TextView) findViewById(R.id.name);
if(PrefUtils.getCurrentUser(MainActivity.this)==null)
{
info.setVisibility(View.INVISIBLE);
profileImgView.setBackgroundResource(R.drawable.wallpaper_for_facebook_profile_photo);
}
else
{
Log.e("sono loggato", "si");
info.setVisibility(View.VISIBLE);
profileImgView.setVisibility(View.VISIBLE);
User user= PrefUtils.getCurrentUser(MainActivity.this);
Log.e("urlImage", user.profileName);
Glide.with(MainActivity.this)
.load(user.imageUrl)
.into(profileImgView);
if (Profile.getCurrentProfile() != null) {
info.setText(Profile.getCurrentProfile().getName());}
}
mainactivity.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_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
nav_header_main.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="#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/thumb_img"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/wallpaper_for_facebook_profile_photo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/name"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports News"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/date_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I think you might need to find your TextView from the NavigationView.
so try to change
info = (TextView) findViewById(R.id.name);
to
info = (TextView) navigationView.findViewById(R.id.name);
The root cause is that you do not have TexView with id name in your xml referenced by R.layout.activity_main, so this find fails:
info = (TextView) findViewById(R.id.name);

Changing Text from header xml

I have an activity in which upon loading I want to update a textfield. That textfield is stored in a header for that activity's layout in an external XML file. When trying to set text pointed at that XML file, I get a nullpointer exception.
Here is the activity in which I am using:
public class NavDrawerMain extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//JAC - need to add big search box right at the top of the drawer
//stuff for the user Timeline
private CustomAdapter timelineAdapter;
private ListView timelineList;
private PullToRefreshView mPullToRefreshView;
public static final int REFRESH_DELAY = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_drawer_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
TextView userName = (TextView) findViewById(R.id.drawerUserName);
userName.setText("test");
Here is the layout for that activity:
<RelativeLayout
android:layout_width="match_parent"
android:background="#e5e5e5"
android:layout_height="wrap_content">
<com.yalantis.phoenix.PullToRefreshView
android:id="#+id/pull_to_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/timeListView"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#null"
android:dividerHeight="0dp"/>
</com.yalantis.phoenix.PullToRefreshView>
</RelativeLayout>
<include layout="#layout/app_bar_nav_drawer_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_nav_drawer_main"
app:menu="#menu/activity_nav_drawer_main_drawer" />
and lastly here is the external xml file that houses the TextView that I want to change. This file is the header of the activity's xml file
<?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="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
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" android:orientation="vertical"
android:gravity="bottom">
android:id="+id/RelativeLayout1"
<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" android:id="#+id/imageView" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing" android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="android.studio#android.com" android:id="#+id/drawerUserName" />
the textviews id is drawerUserName. I had read somewhere that I need to inflate the layout but haven't been able to figure it out.
In your activity inflate header_layout like this
View headerView = LayoutInflater.from(this).inflate(R.layout.header_layout, null);
TextView userName = (TextView) headerView.findViewById(R.id.drawerUserName);
userName.setText("test");
Now add it as a HeaderView to NavigationView
navigationView.addHeaderView(headerView);
And that should be it!
For smiliar question check;
NavigationView how to handle dynamic header content

Android Api 23 Change Navigation View headerLayout textview

I am testing the Navigation Drawer sample project in android and i have a problem setting the text in navigation view profile header.
This is my code:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
TextView text = (TextView) findViewById(R.id.textView);
texto.setText("HELLO");
}
activity_main.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_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="#layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<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_main" app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
nav_header_main.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="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
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" android:orientation="vertical"
android:gravity="bottom">
<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" android:id="#+id/imageView" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing" android:text="Android Studio"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="android.studio#android.com" android:id="#+id/textView" />
</LinearLayout>
When i try to set the text of the textview i always get this error and it only happens with api level 23:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
How can i change the Textview text of the nav_header_main.xml from the main activity??
Thanks in advance
Now with the 23.1.1 release of the design support library, you can use
View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);
I had the same issue and was able to avoid it with this code:
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
navigationView.addHeaderView(header);
TextView text = (TextView) header.findViewById(R.id.textView);
text.setText("HELLO");
Orium answer works and this will work as well.
View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
TextView text = (TextView) header.findViewById(R.id.textView);
texto.setText("HELLO");
this solved the problem for me
View headerView = navigationView.inflateHeaderView(R.layout.header_view);
txt_fullname = (TextView) headerView.findViewById(R.id.txt_fullname);
txt_email = (TextView) headerView.findViewById(R.id.txt_email);
View headerView=navigationView.getChildAt(0);
TextView tvName=(TextView)hView.findViewById(R.id.name);
TextView tvEmail=(TextView)hView.findViewById(R.id.email);
tvName.setText(data);
tvEmail.setText(data);
Or if you use this attribute:
<android.support.desibn.widget.NavigationView>
...
app:headerLayout="#layout/your_header_layout"/>
You can:
View header = mNavigationView.getHeaderView(0);
TextView title = (TextView) header.findViewById(R.id.your_title_id);
//Or something else
The above answer is right but in kotlin, you need to use findViewById<View>
val navigationHeader = navigation_view.getHeaderView(0)
val navigationTitle = navigationHeader.findViewById<View>(R.id.txt_navigation_title) as TextView
navigationTitle.text = "This is your header"

Categories

Resources