Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DataBinding Custom Binding Adapter Warning

I followed the data binding documentation for Custom Binding Adapter for image loading from official Android Developers site: http://developer.android.com/tools/data-binding/guide.html

After successfully compiling the code I get a warning which is:

Warning:Application namespace for attribute bind:imageUrl will be ignored. 

My Code is as follow:

@BindingAdapter({"bind:imageUrl"})     public static void loadImage(final ImageView imageView, String url) {         imageView.setImageResource(R.drawable.ic_launcher);         AppController.getUniversalImageLoaderInstance().displayImage(url, imageView);     } 

Why this warning is generated?

A screenshot is also attached...enter image description here

like image 941
asad.qazi Avatar asked Feb 10 '16 11:02

asad.qazi


People also ask

What is binding adapter in Android?

Binding adapters are responsible for making the appropriate framework calls to set values. One example is setting a property value like calling the setText() method. Another example is setting an event listener like calling the setOnClickListener() method.

Is Data Binding important Android?

Data Binding allows you to effortlessly communicate across views and data sources. This pattern is important for many Android designs, including model view ViewModel (MVVM), which is currently one of the most common Android architecture patterns.

What is data binding in Android Studio?

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. Layouts are often defined in activities with code that calls UI framework methods.


2 Answers

I believe the namespace is truly ignored in the BindingAdapter annotation. The warning occurs if you use any namespace prefix, regardless of whether it matches the one used in your layout or not. If you omit the namespace, like so:

@BindingAdapter({"imageUrl"}) 

...the warning doesn't occur.

I suspect the warning exists to alert us the namespace is stripped off before the string is used as a key in the annotation's implementation. Which makes sense when you consider layouts are free to declare whatever namespaces they want, e.g. app: or bind: or foo:, and the annotation needs to work across all those cases.

like image 194
Lorne Laliberte Avatar answered Oct 09 '22 02:10

Lorne Laliberte


Actually there are still some tutorials which add prefix to the BindingAdapter annotation.

Use @BindingAdapter({"imageUrl"}) without any prefix.

<ImageView     imageUrl="@{url}"     android:layout_width="wrap_content"     android:layout_height="wrap_content" /> 

Pro Tip

You will not get warning when using android: prefix in BindingAdapter. Because that is encouraged. I will suggest to use @BindingAdapter("android:src") instead of creating a new attribute.

@BindingAdapter("android:src") public static void setImageDrawable(ImageView view, Drawable drawable) {     view.setImageDrawable(drawable); } 

and

@BindingAdapter("android:src") public static void setImageFromUrl(ImageView view, String url) {    // load image by glide, piccaso, that you use. } 

Create a new attribute only when you need it.

like image 39
Khemraj Sharma Avatar answered Oct 09 '22 04:10

Khemraj Sharma