3

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>
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
Daniel Kng
  • 786
  • 1
  • 9
  • 21
  • 1
    You can refer to this post http://stackoverflow.com/a/33699825/2308683 – OneCricketeer Jun 11 '16 at 20:29
  • Worked! Thanks! Do you know any way to "refresh" the UI? Since i change the Username trough the Preferences the App needs to restart to accept the Changes. Thanks! – Daniel Kng Jun 11 '16 at 20:35
  • 1
    I think I understand what you're asking, but the only way I can think of is overriding `onResume` of the MainActivity to update that value. – OneCricketeer Jun 11 '16 at 20:37

2 Answers2

5

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);
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
0

As I am able to understand, you need to first get the DrawerLayout object, and then run mDrawerLayout.findViewById(R.id.usernameHeader); But remember to setup drawerLayout before doing the findViewById on DrawerLayout.

Your header should be in your main activity. I am attaching a sample xml for reference.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/toolbar_home"
            android:layout_width="match_parent"
            android:layout_height="@dimen/toolbar_height"
            android:background="@color/red">

            <include
                layout="@layout/toolbar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/toolbar_height"
                android:elevation="5dp" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar_home">
        </RelativeLayout>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="260dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff"
        android:orientation="vertical"
        android:scrollbars="none">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="180dp">

            <ImageView
                android:id="@+id/user_banner"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#AA000000" />

            <LinearLayout
                android:id="@+id/topView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <fandooo.com.fandooo.wrapper.CircleImageView
                        android:id="@+id/circularView"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:layout_below="@+id/imageView"
                        android:layout_centerHorizontal="true"
                        android:src="@drawable/profile_photo_silhouette_48dp" />

                    <TextView
                        android:id="@+id/text_fullname"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:paddingTop="5dp"
                        android:text="----"
                        android:textColor="#ffffff"
                        android:textSize="15sp" />

                    <TextView
                        android:id="@+id/text_fanscore"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:paddingTop="5dp"
                        android:text="Fan Score 0"
                        android:textColor="#ffffff"
                        android:textSize="15sp" />

                </LinearLayout>

            </LinearLayout>

        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/menuItemList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/topView"
            android:layout_gravity="left"
            android:scrollbars="none" />
    </LinearLayout>


</android.support.v4.widget.DrawerLayout>

Here you can see, all the views belongs to the main activity. In your case the xml having usernameHeader now where attached to your main activity.

Hope it will work :)

Neo
  • 3,546
  • 1
  • 24
  • 31