Data binding adapter. How to get view by id reference?

view binding vs data binding
android view binding
binding adapter android kotlin
android data binding string
android-databinding string resource id
kotlin view binding
android data binding default value
android data binding include layout

I want to specify view reference as atribute to ImageView

@BindingAdapter(value = {"imageUrl", "progressView"}, requireAll = false)
public static void setImageUrl(ImageView imageView, String url, @IdRes int progressBar) {
    Context context = imageView.getContext();
    View progressView  = imageView.getRootView().findViewById(progressBar);
     progressView.setVisibility(View.VISIBLE);

    Glide.with(context).load(url).listener(new RequestListener<String, GlideDrawable>() {
        @Override
        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
            if (progressView!= null) {
                 progressView.setVisibility.setVisibility(View.GONE);
            }
            return false;
        }

        @Override
        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
            if (progressView!= null) {
                 progressView.setVisibility(View.GONE);
            }
            return false;
        }
    }).crossFade().into(imageView);
}

however my progress view is null. I tried cast context to activity and findViewById also null Another solution is just add progress view below Image and when it loads successfully it should be overlayed bt image

Strange that you're seeing a null progressView. You may not have it in your layout maybe?

In any case, you can do this instead:

<ProgressBar android:id="@+id/progressBarView" .../>
<!-- other stuff -->
<ImageView app:progressView="@{progressBarView}" app:imageUrl="..." .../>

And in your BindingAdapter:

@BindingAdapter(value = {"imageUrl", "progressView"}, requireAll = false)
public static void setImageUrl(ImageView imageView, String url, ProgressBar progressBar) {
    ...
}

Binding adapters, Differences from findViewById; Comparison with data binding. View Each binding class contains references to the root view and all views that have an ID. AdapterView is a ViewGroup that displays items loaded into an adapter. The most common type of adapter comes from an array-based data source. This guide shows how to complete several key steps related to setting up an adapter. Fill the layout with data. To add data into the layout that you create in your app's UI, add code similar to the following:

@BindingAdapter "value" is a array of Strings, so try this:

@BindingAdapter(value = {"imageUrl", "progressViewId"}, requireAll = false)
public static void setImageUrl(ImageView imageView, String iamgeUrl, String progressViewStrId) {
     int progressBarId = Integer.valueOf(progressViewStrId);
     ...
}

View Binding, New in Android Studio 3.6, view binding gives you the ability to replace layouts such as Fragment , Activity , or even a RecyclerView Adapter (or ViewHolder ). the properties provided to reference any view in the layout with an id. use a <​layout> tag will use data binding to generate binding objects. Note: Your binding adapters override the default data binding adapters when there is a conflict. Binding adapter methods may optionally take the old values in their handlers. A method taking old and new values should declare all old values for the attributes first, followed by the new values, as shown in the example below:

In my example I pass a ScrollView id as parameter for the BindingAdapter.

This is the layout code:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:fadeVisible="@{viewModel.shippingMethodsVisible}"
    app:fadeScrollView="@{scrollView}">

And this is the Bindig Adapters code written in Kotlin:

@JvmStatic @BindingAdapter(value = ["fadeVisible", "fadeScrollView"], requireAll = false)
fun setFadeVisible(view: View, visible: Boolean, scrollView: ScrollView) {
...

Use view binding to replace findViewById, Make Data Binding Do What You Want My app has a custom View, ColorPicker​: I would love to set the color with data binding. The BindingAdapter annotation takes the attribute name as its parameter. data binding framework can figure out what to do with lambda methods and method references: Data Binding significantly reduces the boilerplate code. Here, we’ll learn how to implement DataBinding with a RecyclerView which has the ViewHolder pattern. Also, we’ll understand how Data Binding makes it easy to generalise the Adapter classes. Finally, we’ll demonstrate how to directly pass the adapter object in the XML.

Android Data Binding: Custom Setters - Android Developers, To get started with data binding, we need to make sure to upgrade to the latest it can still be referenced in Android Studio thanks to integrated support for data binding. binding object; View IDs within a layout become variables inside the binding @BindingAdapter({"isVisible"}) public static void setIsVisible(View view​,  The Data Binding Library works seamlessly with ViewModel components, which expose the data that the layout observes and reacts to its changes. Using ViewModel components with the Data Binding Library allows you to move UI logic out of the layouts and into the components, which are easier to test. The Data Binding Library ensures that the views

Applying Data Binding for Views, This view element contains your root hierarchy similar to layout files which are not used with container, attachToContainer); // get the root view View view = binding. Data binding for events via listener bindings and method references <ImageView android:id="@+id/icon" android:layout_width="40dp"  Binding adapters are often a point of confusion for those new to data binding, and it’s easy to see why. They are pretty easy for simple cases, but tricky to get right when doing something slightly more complex. Once you throw two-way binding into the mix, things can become overwhelming fast.

Using data binding in Android - Tutorial, I have been playing with data binding more and more over the last couple of weeks. This week, it's all R.string.ok, null) .setNegativeButton("Not now", null); View view You can then use this BindingAdapter in the TextView :. In this video I'm going to show you how to get started with Data Binding on Android. We'll go over topics like: 1) Data binding library requirements 2) Binding an Activity layout 3) Passing

Comments
  • This is a great answer. I did have an issue with using progress_bar_view for the id and trying to put that into the app:progressView, but when databinding occurs, id's turn into camelcase.