Search code examples
androidkotlinandroid-databinding

Not able to set background of imageview using databinding


I am trying to set the background of the imageview in my recyclerview but it's not showing anything only empty space for images and when I use a placeholder image it shows the place holder image but not the image URLs I want to show in imageview

My code

@BindingAdapter("imageName")
fun setImageFromURls(view: ImageView, fileName: String) {
    Glide.with(view.context)
            .load(fileName)
            .into(view)
}

recyclerview

 <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/roundedImageView"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:adjustViewBounds="true"
            app:imageName="@{productitem.image}"
            android:scaleType="fitXY"
            app:shapeAppearanceOverlay="@style/roundimageview" />

Product

@Parcelize
data class Product(

        val image: String
        ):Parcelable

Dataprovider

object DataProvider {

    val productList: MutableList<Product> = ArrayList()

    private fun addProduct(imageUri: String) {

        val item = Product(imageUri)
        productList.add(item)


    }
    init {
        addProduct("https://images.unsplash.com/photo-1598128558393-70ff21433be0?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1922&q=80")
        addProduct("https://images.unsplash.com/photo-1598128558393-70ff21433be0?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1922&q=80")
        addProduct("https://images.unsplash.com/photo-1581090700227-1e37b190418e?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80")
        addProduct("https://images.unsplash.com/photo-1581092918056-0c4c3acd3789?ixlib=rb-1.2.1&ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&auto=format&fit=crop&w=1050&q=80")
        addProduct("https://images.unsplash.com/photo-1473968512647-3e447244af8f?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1950&q=80")

    }
}

Solution

  • @BindingAdapter("imageName")
    fun setImageFromURls(view: @BindingAdapter("imageName")
    fun setImageFromURls(view: ShapeableImageView, fileName: String) {
        Glide.with(view.context)
                .load(fileName)
                .into(view)
    }
    

    Using ShapeableImageView in place of ImageView solved the problem for me