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!