0

I have a drawable that's being displayed in a layout. The drawable has default properties such as color and width. I'm creating a data class to be able to update those when the recycler view is created. The part that fills in the color in the middle is working. But I'm stuck on how to actually update the drawable width and the ImageView margins from the viewHolder.

mydrawable.xml

The layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/rect_outline"
        android:src="@drawable/mydrawable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginStart="12dp"
        android:layout_marginEnd="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>

The data class

data class ImageData(
    val c: String
    val width: Int
    val marginLeft : Int

)

MyViewHoler.kt

internal class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(item: ImageData) {
   itemView.rect_outline.setColorFilter(Color.parseColor(item.c))

}

}

  • Does this answer your question? [Set ImageView width and height programmatically?](https://stackoverflow.com/questions/3144940/set-imageview-width-and-height-programmatically) – Dharmaraj Apr 26 '20 at 15:54
  • No, unfortunately, it doesn't. I'm not trying to change the Imageview width only it's margins. On the other hand, I do want to update the drawable width – PizzaParty123 Apr 26 '20 at 16:18

1 Answers1

0

first put this into your xml image view component

android:scaleType="fitCenter"

my sugest is use scale with animation to get a good looking for the user

imageView.animate()
                    .scaleX(scaleValue)
                    .scaleY(scaleValue)
                    .setDuration(MOVING_ANIMATION_DURATION)
                    .start()
imageView.invalidate()

but you can set a new layout params like:

val l =  imageView.layoutParams
            l.height =  value
            l.width = value
            imageView.layoutParams = l

you can do this way too

    val params = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )
            params.setMargins(0, 20, 0, 40)
            params.height = 1
            params. width = 1
            imageView.layoutParams = params
Felipe Custódio
  • 102
  • 4
  • 13