21

I have a layout that includes another layout:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

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

    <include layout="@layout/included_layout" />

</LinearLayout>

included_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <TextView
        android:id="@+id/includedTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Included TextView"/>

</LinearLayout>

How can I get a reference to the TextView in the included layout? Is it not supported (yet)?

MainActivity:

import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.activity_main.*

class MainActivity : AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        textView.text = "text" // works!
        textViewInclude.text = "textInclude" // does not work: "Unresolved reference: textViewInclude "
    }
}
Lukas Lechner
  • 7,881
  • 7
  • 40
  • 53

2 Answers2

19

You should import the included layout.

import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.included_layout.* // Here

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // From activity_main.textView
        textView.text = "text" 

        // From included_layout.textViewInclude
        textViewInclude.text = "textInclude"
    }
}
konomae
  • 379
  • 3
  • 5
  • 2
    Can't it simply import automatically? – Dharmendra Pratap Singh Feb 21 '16 at 07:07
  • 2
    im having the same issue, when I import this line: import kotlinx.android.synthetic.main.included_layout.* i get an issue ( unresolved reference : kotlinx ) can you help please ? – elGreato Jul 18 '17 at 15:31
  • 3
    what about if you have two includes of the same layout? – Damia Fuentes Oct 23 '17 at 16:02
  • 9
    doesn't work if the layout is i another module, unfortunately. is there any solution to this? am i missing something? – Євген Гарастович Dec 08 '17 at 08:10
  • my not working still null pointer on other layout view – Akash kumar Jul 17 '20 at 19:42
  • 3
    Although this post is a bit older I want to add for completeness: You need to add and apply the `kotlin-android-extensions` in your `build.gradle` in order to import from your layout directly. Please find how to do this [here](https://stackoverflow.com/questions/34169562/unresolved-reference-kotlinx) – Tobias Oct 23 '20 at 13:54
  • As @Tobias mentioned, you need to apply `kotlin-android-extensions` plugin in the `build.gradle`. Any idea why it isn't applied by default in some cases? – Mohamed Medhat Mar 30 '21 at 09:55
0

In the Activity where you want acess of other layout element implement click Listner

class LoginActivity : AppCompatActivity(), View.OnClickListener {
 
 override fun onClick(view: View?) {
    when (view?.id) {
        R.id.dialog_cancel-> { dialog.cancel() }
    }
  }
}

in the another layout which you are importing in your Activity, place onClick() in the element and refrence it to the Activity where you imported onClick listner android:onClick="onClick"

enter image description here

It will work definetly and the another xml element will listen the click

Akash kumar
  • 981
  • 3
  • 14
  • 27