1

Consider the following code:

binding adapter:

@BindingAdapter("visibility")
fun setVisibility(view: View, shouldBeVisible: Boolean) {
    view.visibility = if (shouldBeVisible) View.VISIBLE else View.GONE
}

what's the difference between using bind namespace like this:

<TextView
        android:id="@+id/text_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        bind:visibility="@{mainViewModel.showTextView}"/>

and using app namespace like this:

<TextView
    android:id="@+id/text_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:visibility="@{mainViewModel.showTextView}"/>

they both work in my code.

glisu
  • 1,027
  • 10
  • 20

2 Answers2

2

App Name-Space

The app namespace is not specific to a library, but it is used for all attributes defined in your app, whether by your code or by libraries you import, effectively making a single global namespace for custom attributes - i.e., attributes not defined by the android system.

In this case, the appcompat-v7 library uses custom attributes mirroring the android: namespace ones to support prior versions of android (for example: android:showAsAction was only added in API11, but app:showAsAction (being provided as part of your application) works on all API levels your app does) - obviously using the android:showAsAction wouldn't work on API levels where that attribute is not defined.

  1. Bind

Bind is use for custom setter in android data binding. For details check below

You simply need to annotate a static method with the BindingAdapter annotation. This Annotation takes a string as a parameter. The string is the custom attribute, this static method will be bound to. Avoid to add the namespace in the annotation parameter as it will make the binding flaky. The first parameter of the method is the View object to apply the function to, and the second parameters is the value retrieved from layout XML.

@BindingAdapter("progressColor")
public static void setProgressBarColor(ProgressBar loader, int color) {
  if (loader != null) {
    loader.getIndeterminateDrawable()
      .setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_IN);
  }
}

Fore more details

<ProgressBar
  style="?android:attr/progressBarStyleLarge"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:indeterminate="true"
  bind:progressColor="@{@android:color/holo_green_dark}"
/>

Details about Binding Adapter check below: https://developer.android.com/topic/libraries/data-binding/binding-adapters

Ankit Tale
  • 1,924
  • 4
  • 17
  • 30
2

They are the exact same, you just gave them different names in your XML file. Your bind namespace probably looks like this:

xmlns:bind="http://schemas.android.com/apk/res-auto"

You could also call this bla or whatever. Using bind just makes more clear what it does.

Florian Walther
  • 6,237
  • 5
  • 46
  • 104