1

I want to set the value of a textfield in an inner layout. This is my parent xml(outer layout).

activity_navigator.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" >

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

nav_header_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/ic_background"
    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">

    <TextView
        android:layout_width="119dp"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:id="@+id/userTextView"
        android:text="text"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>

I wanto set the value of the text field userTextView from navigator.kt class

navigator.kt

       override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_navigator)
//getting exception here
                  val userTextView: TextView= findViewById<TextView>(R.id.userTextView)
           Log.d("usertextview",userTextView.toString())

                }

Please help me solve this.

Amrutha Jaya Raj
  • 612
  • 2
  • 10
  • 32

1 Answers1

6

try this in your navigator.kt

get your navigation headerview and set text to TextView. Like this

    lateinit var  navView:View
    lateinit var  txt_name : TextView

    navView = nav_view.getHeaderView(0)

    txt_name = navView.findViewById(R.id.userName) as TextView
    txt_name.text = "user"
Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51