Yik Jin Yik Jin - 4 months ago 37
Android Question

setText in another layout

So I'm having a little bit of trouble trying to change the text of a

TextView
in another layout. My
MainActivity.java
sets the content view as
activity_main.xml
, which
<include>
s
app_bar_main.xml
, which
<include>
s
content_main.xml
, where my main app layout is displayed (it's a Navigation Drawer Activity in Android Studio).

Part of my code in
MainActivity.java
is shown below:

String user, organisation;

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

// Default code to include navigation drawer here

if (savedInstanceState == null) {
Bundle bundle = getIntent().getExtras();

if (bundle != null) {
user = bundle.getString("user");
organisation = bundle.getString("organisation");

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View hamburgerMenu = inflater.inflate(R.layout.nav_header_main, null);

TextView navUsername = (TextView) hamburgerMenu.findViewById(R.id.nav_username),
navOrgGroup = (TextView) hamburgerMenu.findViewById(R.id.orgGroup);

navUsername.setText(user);
navOrgGroup.setText(organisation);
}
}
}


And my
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:contentDescription="@string/user"
android:src="@android:drawable/sym_def_app_icon" />

<TextView
android:id="@+id/nav_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="@string/user"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
android:id="@+id/orgGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email" />
</LinearLayout>


But for some reason the
TextView
s in
nav_header_main.xml
never changes. It remains as the default values set in the XML file. Why is this so? Thanks in advance!

Answer

Since my comment helped solve:

In my code I'm using somewhat of a different strategy to change the TextView in the navigation drawer. View v = navigationView.getHeaderView(0); TextView emailTextView = (TextView) v.findViewById(R.id.emailTextView);

As for the explanation, you might take a look at this question. It'd be redundant that I copy the text here.