j2emanue
j2emanue

Reputation: 62519

android -data binding how to use

I want to use data binding in my android studio project if its available in production. I am searching online and find references to a beta copy which I clearly dont want. But i read here that data binding is apart of api 23 and built into android studio ? how do i use it if this is true ? I cant find it on jcenter, is it something thats built into the IDE ?

I created a minimum sdk project of 21 and I'd like to use databinding. This code in xml wont compile:

 <TextView android:text="@{user.name}"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:visibility="@user.isAdmin ? View.VISIBLE : View.GONE}"/>

the exact error is on the visibility line and it says "Missing /"

Does it only work on api 23 ? wouldn't this break on older devices then ?

Upvotes: 1

Views: 1699

Answers (2)

A Hashemi
A Hashemi

Reputation: 339

The Data Binding Library offers both flexibility and broad compatibility—it's a support library, so you can use it with devices running Android 4.0 (API level 14) or higher.

However data binding is supported on Android Plugin for Gradle version 1.5.0 and higher. I recommend you to use the latest Plugin for Gradle in your projects.

to get started with data binding see https://developer.android.com/topic/libraries/data-binding/start.html

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006594

I am searching online and find references to a beta copy which i clearly dont want.

It is in a release candidate state at this time.

But i read here that data binding is apart of api 23 and built into android studio ?

Data binding is supplied by the Android Support libraries and a Gradle plugin. It is not related to API Level 23. Android Studio support exists, to some degree.

This code in xml wont compile:

That is because you are missing the binding expression opening characters. Change that attribute to:

android:visibility="@{@user.isAdmin ? View.VISIBLE : View.GONE}"

Does it only work on api 23 ? wouldn't this break on older devices then ?

Quoting the documentation: "you can use it with all Android platform versions back to Android 2.1 (API level 7+)."

Upvotes: 2

Related Questions