2

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 TextViews 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!

  • Since you're not getting any `NullPointerException`s you've initialized the `TextViews` well. Now can you just try `.setText("anything");` instead of `user` since it may be empty or something. – Vucko Jul 16 '16 at 15:32
  • I've added a Snackbar right after setText and I can be sure that `user` and `organisation` are not `null` –  Jul 16 '16 at 15:33
  • 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);` Might you try this approach? – Vucko Jul 16 '16 at 15:47
  • 1
    @Vucko It worked! Do upload your comment as an answer so that I can accept it. Will be good if you can explain the code too :) –  Jul 16 '16 at 15:56
  • Thanks mate. I linked a question that explains this in my answer :) – Vucko Jul 16 '16 at 16:00

2 Answers2

1

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.

Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
0

your hamburgerMenu has inflated but not add to any viewGroup to show it

luckybilly
  • 221
  • 1
  • 9