I'm trying to change android:layout_marginStart and android:layout_marginEnd of and imageView on a button click, but all I can seem to do is one the needs it in px rather than dp. I have it constrained to the left and right of another image when the activity loads, the other image is 300dp wide and the image I want to move has margins set to 150dp and 150dp, I want to be able to set this to anything, currently testing 30dp (start) and 270dp (end) so it would be 10% of the way along from the left.
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Result">
<ImageView
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:id="@+id/bar"
    android:minHeight="50dp"
    android:minWidth="300dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginStart="8dp"
    app:layout_constraintStart_toStartOf="parent"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent"
    android:backgroundTint="#00050505"/>
<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    app:srcCompat="@drawable/ic_arrow_drop_up_black_24dp"
    android:id="@+id/imageView4"
    app:layout_constraintStart_toStartOf="@+id/bar"
    app:layout_constraintEnd_toEndOf="@+id/bar"
    android:layout_marginStart="150dp"
    android:layout_marginEnd="150dp"
    app:layout_constraintTop_toTopOf="@+id/bar"
    app:layout_constraintBottom_toBottomOf="@+id/bar"
    android:layout_marginTop="40dp"/>
<Button
    android:text="Change"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/bar"
    android:layout_marginBottom="8dp"
    android:onClick="changeValue" android:layout_marginStart="8dp"
    app:layout_constraintStart_toStartOf="parent"     
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin file:
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.result)   
}
fun changeValue(view:View){
        imageView4.updateLayoutParams<ConstraintLayout.LayoutParams> {
        marginEnd = 270
        marginStart = 30
    }
}
setting the marginEnd and marginStart like this is asking for pixels rather than dp, so it's different for all phones, is there a way to change the 150dp (start) and 150dp (end) to something else?
Thanks
 
    