Daniel Kng Daniel Kng - 5 months ago 56
Android Question

Change TextView from another Layout

For my latest Project in Android, i need to set a TextView that's in

R.layout.header
(The content for my NavDrawer, see Screenshot below) from my MainActivity, which uses the activity_main layout.

To do that I call my
SetUsername()
Method which contains the following code to get the Username from the Preferences:

private void SetUsername(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String userNamePref = preferences.getString("username", "DEFAULT");
//Change the Username in R.layout.header
}


To set the Username i first did

TextView username = (TextView)findViewById(R.id.usernameHeader);
username.setText(userNamePref);


which did not work - obviously, because R.id.usernameHeader is in the R.layout.header.

So I used this:

setContentView(R.layout.header);
TextView username = (TextView)findViewById(R.id.usernameHeader);
username.setText(userNamePref);


Which changed the Text in my NavDrawer correctly, but the activty_main layout was gone, so i added
setContentView(R.layout.activity_main)
at the bottom of my function. And everything works as intended, but the Text hasn't changed.

To get a better idea I made a Screenshot so you can see what exactly I want to change:
NavDrawer

My Layouts:

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:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_screen3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:id="@+id/linearLayout">

<ImageView
android:layout_width="@dimen/img_width_height"
android:layout_height="@dimen/img_width_height"
android:src="@drawable/ic_assessment_white_36dp"/>

<TextView
android:id="@+id/stundenplanText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/slide_2_title"
android:textColor="@android:color/white"
android:textSize="@dimen/slide_title"
android:textStyle="bold" />

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/class_spinner"
android:layout_below="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_start"
android:id="@+id/okay_button"
android:elevation="5dp"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp">
</Button>
</LinearLayout>
<include
android:id="@+id/tool_bar"
layout="@layout/toolbar">
</include>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>


header:

<?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="190dp"
android:background="@color/bg_screen3"
android:orientation="vertical"
android:id="@+id/headerRelative">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BT-Ahaus"
android:textSize="14sp"
android:textColor="#FFF"
android:textStyle="bold"
android:gravity="left"
android:paddingBottom="4dp"
android:id="@+id/usernameHeader"
android:layout_above="@+id/email"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bt-ahaus@justanotherschool.de"
android:id="@+id/email"
android:gravity="left"
android:layout_marginBottom="8dp"
android:textSize="14sp"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/usernameHeader"
android:layout_alignStart="@+id/usernameHeader" />

<ImageView
android:id="@+id/profile_image"
android:layout_width="150dp"
android:layout_height="100dp"
android:src="@drawable/bt_ahaus"
android:layout_centerVertical="true"
android:layout_alignEnd="@+id/email" />

</RelativeLayout>


toolbar:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/bg_screen3"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:elevation="4dp">

</android.support.v7.widget.Toolbar>

Answer

Switching the ContentView is completely unnecessary.

To find a View on the layout of your header, you need to call findViewById on that particular layout's instance.

Instead of the following:

TextView username = (TextView)findViewById(R.id.usernameHeader);

You should do the following:

NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0);
TextView username = (TextView) header.findViewById(R.id.usernameHeader);