Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing width and height in ConstraintLayout programmatically doesn't work

I would like to change the following UI to be full screen programmatically.

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ONE"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/two"

        />

    <TextView
        android:id="@+id/two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TWO"
        android:background="@android:color/holo_blue_dark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/one"
        />

</android.support.constraint.ConstraintLayout>

In my Kotlin file:

class MainActivity : AppCompatActivity() {

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

        val param = ConstraintLayout.LayoutParams(0, 0)

        one.layoutParams = param
        two.layoutParams = param
    }
}

Both TextView disappears after I set the width and height to 0 instead of being scratched fullscreen.

Desired UI enter image description here

like image 877
Xi Wei Avatar asked Jul 12 '18 06:07

Xi Wei


People also ask

How do I change ConstraintLayout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.

Is ConstraintLayout better than RelativeLayout?

ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI. Where in the Relative layout you needed multiple nested layouts (LinearLayout + RelativeLayout).


1 Answers

You're doing it wrong. You've to apply a new ConstraintSet like:

val constraintSet1 = ConstraintSet()
constraintSet1.clone(yourConstraintLayout)

val constraintSet2 = ConstraintSet()
constraintSet2.clone(yourConstraintLayout)
constraintSet2.constrainWidth(R.id.one, 0)
constraintSet2.constrainWidth(R.id.two, 0)
constraintSet2.constrainHeight(R.id.one, 0)
constraintSet2.constrainHeight(R.id.two, 0)

// to switch to full screen
constraintSet2.applyTo(yourConstraintLayout)

// to get back to your original screen
constraintSet1.applyTo(yourConstraintLayout)
like image 110
Benjamin Avatar answered Oct 20 '22 16:10

Benjamin