Home > front end >  How to change button position go down (constraint layout) in kotlin android ? I need space to displa
How to change button position go down (constraint layout) in kotlin android ? I need space to displa

Time:09-18

<Button
        android:id="@ id/heartRateButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:text="@string/heartRate_button"
        android:backgroundTint="@color/midnight_blue"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.647"

So basically what I want to change is the app:layout_constraintVertical_bias="0.647". But I need to be able to change it in the mainActivity.kt file instead of in the XML file.

CodePudding user response:

You can use these lines of code to set vertical bias programmatically

//id of the button
val button: Button = findViewById(R.id.heartRateButton)


val newParam:ConstraintLayout.LayoutParams = 
            (button.layoutParams as ConstraintLayout.LayoutParams)
newParam.verticalBias = 1.0f
        
button.layoutParams = newParam

CodePudding user response:

Instead of moving the Button yourself, you might want to place the message TextView in the layout where the Button is now, and constrain the Button to sit below it

<TextView
        android:id="@ id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/some_message"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.647"
/>

<Button
        android:id="@ id/heartRateButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="4dp"
        android:text="@string/heartRate_button"
        android:backgroundTint="@color/midnight_blue"
        android:layout_marginTop="8dp"
        app:layout_goneMarginTop="0dp"
        app:layout_constraintTop_toBottomOf="message"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
/>

So the message box is displayed where the Button was, and the Button is constrained below it with a top margin set. But there's also the layout_goneMarginTop attribute, which gives it a different margin when the thing it's constrained to has GONE for its visibility.

So basically, when the message box is set to GONE, it takes up no space in the layout, and the button moves up to where the message box would be, with no margin. When the message box is set to VISIBLE, it appears, and the button slides down below it, and applies a top margin to give it some space between the two. You just have to hide or show the message box in code by setting its visibility, the constraints do the rest!


If the message box isn't supposed to be exactly where the button is, you could try something with a Barrier constrained to the bottom of message and maybe a Guideline (or an empty View) where the Button should be. Set it so the barrier is pushed down to the lower of those two things, and constrain your button to that. That way, if the message box isn't visible, the Guideline or whatever is lower, and the Button fixes to that. If the message becomes visible, it pushes the barrier lower and moves the button down where it needs to be

  • Related