Set Text from header textview widget

1k Views Asked by At

I try to implement in the header navigation view data obtained from google account. ie name and email.

by simply setText receive erroare widget. but by RemoteViews not going to change the text. Can solve? I did something wrong ?

Code Activity:

public class Main2Activity extends AppCompatActivity  implements VideosFragment.Contract {

    private Toolbar toolbar;
    //Defining Variables
    private NavigationView navigationView;
    private DrawerLayout drawerLayout;

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

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.header);
        remoteViews.setTextViewText(R.id.username, "email here");

header.xml:

    <?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="@drawable/background_material"
    android:orientation="vertical"
    >

    <de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="76dp"
    android:layout_height="76dp"
    android:src="@drawable/profile"
    app:border_color="#FF000000"
    android:layout_marginLeft="24dp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginStart="24dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GrupoVRT"
        android:textSize="14sp"
        android:textColor="#FFF"
        android:textStyle="bold"
        android:gravity="left"
        android:paddingBottom="4dp"
        android:id="@+id/username"
        android:layout_above="@+id/email"
        android:layout_alignLeft="@+id/profile_image"
        android:layout_alignStart="@+id/profile_image" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VRT Player (m3u8)"
        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/username"
        android:layout_alignStart="@+id/username" />

</RelativeLayout>

enter image description here

and "Main2Activity" is the Activity at which launches on startup and all the activity going on here.

activity_main2

    <?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=".Main2Activity"
    android:background="@drawable/back">
    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">
        <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/toolbar"
            android:id="@+id/include" />


        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/bShare"
                android:layout_width="91dp"
                android:layout_height="wrap_content"
                android:src="@android:drawable/ic_menu_share"
                android:layout_gravity="bottom|right"
                android:layout_marginRight="16dp"
                android:layout_marginBottom="16dp" />
        </FrameLayout>

    </LinearLayout>

    <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>
1

There are 1 best solutions below

1
ribads On BEST ANSWER

I was hoping you would put up the source layout of the activity_main2. But nonetheless, if you are using RecyclerView and you referenced "header" layout as a headerLayout, you might want to access the View object using

    NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
    View headerView = navigationView.getHeaderView(0);
    TextView mUsernameView = (TextView) headerView.findViewById(R.id.username);
    mUsernameView.setText("username text");

You can now manipulate the header view object using the "headerView" reference. This is primarily because the NavigationView implication was changed from ListView to RecyclerView from support appcompat v7:23.1