0

So, this should be pretty basic, but I am new to Kotlin. So I basically have the following ImageView inside a RelativeLayout view.

<ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            app:srcCompat="@drawable/moon_0"
            android:id="@+id/imagen_luna"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp" 
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true" 
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true" />

The thing is that under certain condition this ImageView can collide visually with another element, so I have to modify the marginStart and the marginEnd properties. I saw in some tutorials that first I must obtain the current layout margins or something like that, but I am not sure why it's so complicated, in Swift I just modify the properties of each margin.

Is there an easy way to achieve this or in case there isn't what would be the easiest way to do this?

UPDATE - Added code in the Adapter

This is the code I have in my adapter for this specific ImageView:

override fun onBindViewHolder(holder: AnoViewHolder, position: Int) {
    ...
    if(respec.nombre_icono_signo != "" && respec.imagen_luna != "") {
        val layoutParamsLuna = holder.view?.imagen_luna.layoutParams as RelativeLayout.LayoutParams
        layoutParamsLuna.setMargins(1, 8, 15, 8)
        holder.view?.imagen_luna.layoutParams = layoutParamsLuna
    }
    ...
}

The thing is that this does nothing and my theory is that the setMargins function accepts left and right margins, not start and end margins.

Mihail Minkov
  • 2,463
  • 2
  • 24
  • 41

1 Answers1

1

You can update margin in layoutParams of view

Ex:

    val layoutParams= imagen_luna.layoutParams as RelativeLayout.LayoutParams
    layoutParams.marginEnd=resources.getDimension(your_dimension).toInt()
    layoutParams.marginStart=resources.getDimension(your_dimension).toInt()

    layoutParams.bottomMargin=resources.getDimension(your_dimension).toInt()
    layoutParams.topMargin=resources.getDimension(your_dimension).toInt()

    //or

    layoutParams.setMargins(start,top,end,bottom)

    imagen_luna.layoutParams=layoutParams

You have to add value for margins in dimension file.because if you set setMargins(1, 8, 15, 8) like this,It's will margin in pixel not in dp.

So use margin values like below.

<dimen name="spacing_normal">16dp</dimen>
<dimen name="spacing_small">8dp</dimen>
<dimen name="spacing_tiny">4dp</dimen>

Update your dapter class like belo

override fun onBindViewHolder(holder: AnoViewHolder, position: Int) {
...
if(respec.nombre_icono_signo != "" && respec.imagen_luna != "") {
    val layoutParamsLuna = holder.view?.imagen_luna.layoutParams as RelativeLayout.LayoutParams
    layoutParamsLuna.setMargins(context.resources.getDimension(R.dimen.spacing_normal).toInt(), 
           context.resources.getDimension(R.dimen.spacing_normal).toInt(), 
           context.resources.getDimension(R.dimen.spacing_normal).toInt(),
           context.resources.getDimension(R.dimen.spacing_normal).toInt())
    holder.view?.imagen_luna.layoutParams = layoutParamsLuna
}else{
  //reset margin for else case because recylerview reuse views so it's will keep previous states
}
...
}
Rajasekaran M
  • 2,478
  • 2
  • 20
  • 30
  • Ok, so from what I see basically, the second example would be something like: `val layoutParams = imagen_luna.layoutParams as RelativeLayour.LayoutParams`, then `layoutParams.setMargins(2, 8, 14, 2)` and then `imagen_luna.layoutParams = layoutParams`. What happens if I want to leave some of the margins to null? – Mihail Minkov May 08 '19 at 14:54
  • Hmm, I don't know if it's relevant, in my case the `ImageView` is in a reusable cell for a `GridLayout` and I modify it in my adapter, so my resulting code is something like this: `val layoutParamsLuna = holder.view?.imagen_luna.layoutParams as RelativeLayout.LayoutParams` then `layoutParamsLuna.setMargins(1, 8, 15, 8)` then `holder.view?.imagen_luna.layoutParams = layoutParamsLuna`, but I notice no changes in the result. Do I need to redraw something or? – Mihail Minkov May 08 '19 at 15:42
  • The only thing that occurs to me is that the `setMargins` function takes as parameters left and right and I am using start and end as usually they are recommended by Android Studio. – Mihail Minkov May 08 '19 at 15:48
  • yes ,you have to use start and end. use margin values from dimension and set margin in `onBindHolder()` – Rajasekaran M May 09 '19 at 04:50
  • I updated my question with code from the Adapter @Rajasekaran M – Mihail Minkov May 09 '19 at 16:40
  • 1
    Hello @Rajasekaran M, it did work, but I had to change start and end to left and right, apparently this specific function is not adapted to start and end and rather changes left and right. Once I had that switched it worked, you could update your answer with that. – Mihail Minkov May 11 '19 at 20:47