Android DataBinding Custom Binding Adapter Warning

23,372

Solution 1

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.

Solution 2

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.

Solution 3

Try this, work for me!. I hope this can help you. Simple way to change image resource without binding adapter.

<ImageButton
        ...
        android:id="@+id/btnClick"
        android:onClick="@{viewModel::onClickImageButton}"
        android:src="@{viewModel.imageButton}" />

and View Model Class :

public ObservableField<Drawable> imageButton;
private Context context;

//Constructor
public MainVM(Context context) {
    this.context = context;
    imageButton = new ObservableField<>();
    setImageButton(R.mipmap.image_default); //set image default
}

public void onClickImageButton(View view) {
    setImageButton(R.mipmap.image_change); //change image
}

private void setImageButton(@DrawableRes int resId){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        imageButton.set(context.getDrawable(resId));
    }else{
        imageButton.set(context.getResources().getDrawable(resId));
    }
}
Share:
23,372
asad.qazi
Author by

asad.qazi

Android Developer, also love to code on iOS. Worked with Dagger2, RxJava, RxAndroid and Android Architecture components. Love to write using AOP and to play with archs. Also have passion to write api's with spring boot and mongo

Updated on July 05, 2022

Comments

  • asad.qazi
    asad.qazi almost 2 years

    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

  • Lorne Laliberte
    Lorne Laliberte about 8 years
    Note that the android: namespace is the only exception: if you're setting a BindingAdapter for a built-in attribute such as android:onClick, you can include the prefix and you will not get any warning.
  • ruX
    ruX over 2 years
    ViewModel should not use any Android-specific classes(especially views) nor have access to context as that defeats the purpose of having it at the first place