Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Kotlin's isNullOrBlank() function be imported into xml for use with data binding

Via data-binding I am setting the visibility of a text field. The visibility is depending on a string being null or empty or nothing of the both.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <import type="android.view.View"/>

        <variable
            name="viewModel"
            type="com.example.viewModel"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        <TextView
            android:id="@+id/textField1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{viewModel.data.text}"
            android:visibility="@{(viewModel.data.text == null || viewModel.data.text.empty) ? View.GONE : View.VISIBLE}"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/> 

    </android.support.constraint.ConstraintLayout>

Is it possible to create an import in the data element so that I can use the isNullOrBlank() function from the kotlin.text.StringsKt class?

It was hoping to be able to use it like this: android:visibility="@{(viewModel.data.title.isNullOrBlank() ? View.GONE : View.VISIBLE}"

like image 432
KraffMann Avatar asked Aug 23 '18 12:08

KraffMann


2 Answers

Android data binding still generates the Java code from the XML instead of the Kotlin code, once data binding will be migrated to generating Kotlin code instead of Java I believe we will be able to use Kotlin extension function in the XML, which will be really cool.

I am sure that gonna happen real soon as Google is pushing to Kotlin heavily. But for now, you have below

TextUtils.isEmpty() as mentioned by @Uli Don't forget to write an import.

The reason why you can't you use StringKt.isNullOrBlack in xml:

Below is the code from Kotlin String.kt

@kotlin.internal.InlineOnly
public inline fun CharSequence?.isNullOrEmpty(): Boolean {
    contract {
        returns(false) implies (this@isNullOrEmpty != null)
    }

    return this == null || this.length == 0
}

As you can see it is annotated with @kotlin.internal.InlineOnly which says the Java generated code of this method will be private.

InlineOnly means that the Java method corresponding to this Kotlin function is marked private so that Java code cannot access it (which is the only way to call an inline function without actual inlining it).

It means it can't be called from Java and as data binding generated code is in JAVA it can't be used in data binding either. Thumb rule is what you can access from JAVA you can use that in data binding if not just use the old Java way I would say.

like image 96
Piyush Agarwal Avatar answered Oct 19 '22 00:10

Piyush Agarwal


TextUtils.isEmpty() should do what you want.

like image 20
Uli Avatar answered Oct 19 '22 02:10

Uli