Search code examples
javaandroiddependency-injectionnavigation-drawerbutterknife

How to Bind a TextView which is in an NavigationView in an Activity


We are using android sdk 26 with butterknife 8.8.1

The HomeActivity has a navigation header, the navigation header also contains some text view. Is it possible to bind the TextViews in HomeActivity ?!

Here are some codes which describes my problem

The HomeActivity class has below layout

<android.support.v4.widget.DrawerLayout ....

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        .....
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/activity_navigation_drawer" />



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

The nav_header has some TextView in it

<LinearLayout ....

    <TextView
        android:id="@+id/userinfo_vo_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />


</LinearLayout>

Is it possible to bind the id of userinfo_vo_name text view in HomeActivity, the simple BindView fails

//fails 
@BindView(R.id.userinfo_vo_name)
TextView userinfoName;

Of course, I have called ButterKnife.bind(this); in onCreate method, but it seems not enough.


Solution

  • Try this

    NavigationView navigationView = (NavigationView) findViewById(R.id.yournavigationView);        
    tvProfileName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.tv_nav_userName);
    imgUserProfile = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.profile_image);