Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Android Extensions: How to get a reference to a view in a layout that gets included into another layout?

Tags:

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 "
    }
}
like image 742
Lukas Lechner Avatar asked Nov 25 '15 10:11

Lukas Lechner


People also ask

What is relative layout in Kotlin?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

What is Kotlinx Android synthetic?

The Kotlin Android Extensions is a compiler plugin that allows you to access views from your XML directly in activities, fragments and views using what they refer to as synthetic properties. To use it, ensure that the plugin is enabled in your module's build.gradle file: apply plugin: 'kotlin-android-extensions'


1 Answers

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"
    }
}
like image 119
konomae Avatar answered Oct 04 '22 12:10

konomae