5

Here is my attempted code.

val imageView = ImageView(holder.itemView.context)

holder.itemView.textView.setCompoundDrawablesWithIntrinsicBounds(
        GlideApp.with(holder.itemView.context).load(URL).into(imageView), 0, 0, 0)

I am also not sure about Glide's image target. I have it set as .into(imageView). Since I want to add an image with TextView, there is no ImageView initialized to load the URL image into. My thought was to create one programmatically with

val imageView = ImageView(holder.itemView.context)

An error that I see is that GlideApp.with(holder.itemView.context).load(URL).into(imageView) is not being detected as a drawable.

Jeremy Valdez
  • 81
  • 1
  • 7

2 Answers2

5

In Kotlin you can do as :

 Glide.with(context).load("url").apply(RequestOptions().fitCenter()).into(
        object : CustomTarget<Drawable>(50,50){
            override fun onLoadCleared(placeholder: Drawable?) {
                textView.setCompoundDrawablesWithIntrinsicBounds(placeholder, null, null, null)
            }

            override fun onResourceReady(
                resource: Drawable,
                transition: Transition<in Drawable>?
            ) {
                textView.setCompoundDrawablesWithIntrinsicBounds(resource, null, null, null)
            }
        }
    )
Alok Mishra
  • 1,904
  • 1
  • 17
  • 18
1

you should create imageView in xml layout. and in your case do this

    Glide.with(applicationContext)
                       .load("https://LINK")
                       .listener(object : RequestListener<Drawable> {
                           override fun onLoadFailed(
                                   glideException: GlideException?,
                                   any: Any?,
                                   target: com.bumptech.glide.request.target.Target<Drawable>?,
                                   exception: Boolean
                           ): Boolean {

                               return false
                           }

                           override fun onResourceReady(
                                   drawable: Drawable?,
                                   any: Any?,
                                   target: com.bumptech.glide.request.target.Target<Drawable>?,
                                   dataSource: DataSource?,
                                   resources: Boolean
                           ): Boolean {
                               //check this on how to set drawable to textView
//https://stackoverflow.com/questions/6931900/programmatically-set-left-drawable-in-a-textview
                               //set drawable to textView or do whatever you want with drawable

                               return false
                           }
                       })
                       .submit()
Elias Fazel
  • 2,093
  • 16
  • 20