How to set image url as background of an ImageView using data binding?

android data binding imageview glide
android data binding imageview example
databinding imageview src
android data binding background drawable
android binding adapter multiple parameters
android bindingadapter attribute not found
data binding android
kotlin bindingadapter not working

I am already having a binding adapter to load the image in the ImageView coming from the URL. Now, I need to load background image URL as the background of the Image View and I am using data binding, glide to load images and writing it in Kotlin.

How can I write a binding adapter for the same?

Here is my XML

<androidx.appcompat.widget.AppCompatImageView

        android:id="@+id/ImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        app:backgroundImageUrl="@{item.backgroundImageUrl}"
        app:mainImageUrl="@{item.mainImageUrl}"/>

Try this XML:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image_view_background"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:background="@color/colorPrimary"
        android:alpha="0.4"
        app:mainImageUrl="@{item.backgroundImageUrl}"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <ImageView
        android:id="@+id/image_view_main"
        android:layout_width="230dp"
        android:layout_height="230dp"
        app:mainImageUrl="@{item.mainImageUrl}"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

For checking purpose I just set the alpha for the background image.

Hope this trick will help you.

How to set image url as background of an ImageView using data , 问题: I am already having a binding adapter to load the image in the ImageView coming from the URL. Now, I need to load background image� While building the android app by using the MVVM architecture design pattern with Kotlin, I faced many of issue for example data binding and ViewModel communication etc. I am sharing one of the issues which I faced to load image URL on image view with help of Picasso.


I used the following Binding Adapter and it's working fine.

@BindingAdapter("backgroundImageUrl")
 fun loadBackgroundImage(view: ImageView, imageUrl:String?)
{
Glide.with(view.context).load(imageUrl).into(object:SimpleTarget<Drawable>()
{
override fun onResourceReady(resource: Drawable, transition: Transition<in 
Drawable>?) 
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
 view.background = resource}
 }
 })
 }

Loading Images Using Data Binding - Glide, Step for implementation. Create a new project and enable Data Binding. Add glide dependency on app/build. Add an ImageView in activity_main.xml. Create a variable in the layout file inside the <data/> which will handle the URL of the image. Set the variable on ImageView in the layout file. In this blog, I will explain about Loading Images from a URL Using Glide with the help of Data Binding. In the previous blog, I have explained, How to bind View's using data binding in Android.


You can try as follows

    try {
        ImageView i = (ImageView)findViewById(R.id.image);
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

data binding adapters, You can then create attributes for any setter by using data binding. is called if both imageUrl and error are used for an ImageView object and imageUrl is For example, the android:background attribute of a view expects a� In this android kotlin source code example, we are going to use data binding with ImageView in Android Kotlin. You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel. Below is a step by step source code to use data binding with ImageView in Android Kotlin.


Load Image url on ImageView by using data binding with Picasso in , Let's create the BindableFieldTarget to load the bitmap. public class ProfileViewModel(context: Context, imageUrl : String) { var IMAGE_URL: String? = null var� If you want to load an image from your web URL into Android ImageView in your android application, This blog is for you. This blog gives you a step by step guide to display image from URL with source code for Android Studio. These are all the steps to create a project in Android Studio. Step 1.


How bind an image to ImageView?, I use ImageViews instead of Buttons and want binding some images to Set drawable resource ID in android:src for ImageView using data� Easiest Dynamic method to download and load image from http URL and set inside imageview example tutorial. Loading image through http URL is very easy with the use of AsyncTask android class. So in this tutorial we are loading our website’s logo inside imageview using AsyncTask class.


Set drawable resource ID in android:src for ImageView using data , how to set image using data binding in android android data binding set background color. I'm trying to set drawable resource ID to android:src of ImageView� Adding images the easy way. Things get a bit trickier when you need to pull the image from a URL, often just provided as a string variable. For example, let’s say you are building a news app