Maff
Maff

Reputation: 439

Android Studio, Kotlin, Change Layout_MarginStart and End dynamically

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

Upvotes: 0

Views: 1353

Answers (2)

Maff
Maff

Reputation: 439

using Peter's reply, I figured it out, so easy, I feel so stupid, thank you Peter! I never thought to check .width, I just assumed that would give me it in dp without checking.

val barInPixels = bar.width
val quarter = 0.25 * barInPixels
val threeQuarters = 0.75 * barInPixels

Upvotes: 1

Peter Staranchuk
Peter Staranchuk

Reputation: 1393

There are two workarounds:

  1. Add this margins to dimension resources of your app. And in your activity/fragment use getResources().getDimension(R.dimen.your_margin); to get it

You will set it in dp in file, but getResources().getDimension(...) will return you calculated value in px

See: Load dimension value from res/values/dimension.xml from source code

  1. You can use Anko library. It contain method dip(...). For example dip(150) will return you value of 150 dp in pixels. See: https://github.com/Kotlin/anko

Hope it'll help

Upvotes: 1

Related Questions