Friday, February 28, 2020

android kotlin - ConstraintLayout center in parent programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.constraintlayout.widget.ConstraintSet.*
import androidx.transition.TransitionManager


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Get the widgets reference from XML layout
        val rootLayout = findViewById<ConstraintLayout>(R.id.rootLayout)
        val checkbox = findViewById<CheckBox>(R.id.checkbox)
        val button = findViewById<Button>(R.id.button)

        button.setOnClickListener {
            val constraintSet = ConstraintSet()
            constraintSet.clone(rootLayout)

            // this block will put it horizontally center of parent
            // set check box constraint start to start of parent
            constraintSet.connect(
                checkbox.id, // the ID of the widget to be constrained
                START, // the side of the widget to constrain
                PARENT_ID, // the id of the widget to constrain to
                START // the side of widget to constrain to
            )

            // set check box constraint end to end of parent
            constraintSet.connect(
                checkbox.id, END, PARENT_ID, END
            )


            // this block put it vertically center
            // set check box constraint top to top of parent
            constraintSet.connect(checkbox.id,TOP, PARENT_ID, TOP)

            // set check box constraint bottom to bottom of parent
            constraintSet.connect(checkbox.id, BOTTOM, PARENT_ID, BOTTOM)


            // optionally, apply the constraints smoothly
            TransitionManager.beginDelayedTransition(rootLayout)

            // both blocks will put the check box exact center of parent
            // finally, apply the constraint set to constraint layout
            constraintSet.applyTo(rootLayout)
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:text="Put CheckBox Center In Parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check Me" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples