Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically inflated layout with Kotlin Android Extensions

I have a following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/white"
    android:paddingLeft="20dp"
    android:paddingRight="20dp">

    <TextView
        android:id="@+id/tvErrorTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/background_dark"
        android:textSize="18sp"
        />
    <TextView
        android:id="@+id/tvErrorDesc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textColor="@android:color/darker_gray"
        android:textSize="16sp"
        />
    <TextView
        android:id="@+id/tvAction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="10dp"
        android:layout_gravity="end"
        android:padding="5dp"
        android:textSize="15sp"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:textColor="@android:color/holo_purple"
        />
</LinearLayout>

When I want to use kotlin android extensions outside of activity like below, it doesn't work. I ended up doing findViewById.

...
...
import kotlinx.android.synthetic.main.dialog_error.*
...
...
 val view = LayoutInflater.from(context).inflate(R.layout.dialog_error, null, false)
    val tvErrorTitle = view.findViewById(R.id.tvErrorTitle) as TextView
    val tvErrorDesc = view.findViewById(R.id.tvErrorDesc) as TextView
    val tvErrorAction = view.findViewById(R.id.tvAction) as TextView

It doesn't pull the views directly from xml. How to use it in programetically inflated layout and avoid findViewById?

Note : This question strictly belongs to Kotlin Android Extensions, not the language itself.

Edit I have imported both :

import kotlinx.android.synthetic.main.dialog_error.view.*
import kotlinx.android.synthetic.main.dialog_error.*

But Android Studio still tries to import from R.id and doesn't recognize those two imports. Is there anything missing?

like image 639
Krupal Shah Avatar asked Jun 25 '17 16:06

Krupal Shah


People also ask

What is Inflater in Kotlin?

kotlin.Any. ↳ java.util.zip.Inflater. This class provides support for general purpose decompression using the popular ZLIB compression library. The ZLIB compression library was initially developed as part of the PNG graphics standard and is not protected by patents.

What is inflating layout in Android?

In this context, Inflate means reading a layout XML (often given as parameter) to translate them in Java code. This process happens: in an activity (the main process) or a fragment.


2 Answers

From the docs you linked:

If we want to call the synthetic properties on View (useful in adapter classes), we should also import

kotlinx.android.synthetic.main.activity_main.view.*.

That is, import kotlinx.android.synthetic.main.layout.view.* as well to load the View extension properties.

Then:

val view = LayoutInflater.from(context).inflate(...)
view.tvErrorTitle.text = "test"
like image 137
nhaarman Avatar answered Sep 18 '22 15:09

nhaarman


It returns a view inflated:

layoutInflater.inflate(R.layout.your_layout, null)

See, you can replace this LayoutInflater.from(context) with this layoutInflater when your class extend from a Context superclass

like image 22
Lucas B. Avatar answered Sep 18 '22 15:09

Lucas B.