Home > database >  Setting constraint programatically
Setting constraint programatically

Time:12-19

I am currently working on a collapsable cardview, I am almost finished, but after I open the cardview, the indicator imageview I've made to show arrow down or up is on top of the content, so I want to constraint the top of my Imageview to the bottom of my content.

here is the xml file I made

<androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@ id/layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    app:cardCornerRadius="8dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <!-- inside here resides the content -->
                    <LinearLayout
                        android:id="@ id/linearLayout10"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:padding="10dp"
                        android:paddingHorizontal="10dp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent">


                        <TextView
                            android:id="@ id/txt_title"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/futurah"
                            android:text="@string/synposis"
                            android:textColor="?attr/TextAndIcon"
                            android:textSize="18dp" />

                        <TextView
                            android:id="@ id/txt_description"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/futurah_light"
                            android:text="Özet Yok."
                            android:textColor="?attr/TextAndIcon"
                            android:textSize="13dp" />


                    </LinearLayout>

                    <LinearLayout
                        android:id="@ id/gradient"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/gradient_second_color"
                        android:orientation="vertical"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"/>

                    <!-- the "Image" to indicate arrow down. -->
                    <ImageButton
                        android:id="@ id/imageButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingTop="10dp"
                        android:background="@android:color/transparent"
                        android:src="@drawable/ic_arrowdown"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:tint="?attr/TextAndIcon" />
                </androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

In order to change the View constraints you should do the following:

ConstraintLayout constraintLayout = findViewById(R.id.layout);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.connect(R.id.imageButton, ConstraintSet.TOP, R.id.linearLayout10, ConstraintSet.BOTTOM);
constraintSet.applyTo(constraintLayout);

Depends on what you actually want to do, you just change the data in the connect method. There's also the clear one to remove some specific constraint, for example:

constraintSet.clear(R.id.imageButton, ConstraintSet.TOP);

CodePudding user response:

change the root layout height from match_parent to wrap_content

android:layout_height="wrap_content"
  • Related