Home > database >  Android. ConstraintLayout, how to apply goneMargin
Android. ConstraintLayout, how to apply goneMargin

Time:01-25

I have layout with ConstraintLayout as parent. Inside my constraint layout there are three views: ImageView and two TextView. I need, depending on whether the visibility of the ImageView and the TextView (1) is visible or not, the margin top of the second TextView changes. This is a visual example of what I want to implement: enter image description here

I tried using goneMarginTop = 32 for my second TextView. However, in this case, as soon as the visibility of the first TextView is gone, I get margin between ImageView and TextView = 32 dp but expected 12 dp. I need margin = 32 dp only when ImageView and first TextView are gone. Here is my code:

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@ id/ivImage"
            android:layout_width="0dp"
            android:layout_height="0dp"  
            android:visibility="gone"
            app:layout_constraintDimensionRatio="H,1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:visibility="visible" />

        <TextView         
            android:id="@ id/tvTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="14dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="14dp"
            android:gravity="center"
            android:textColor="@color/black"
            android:textSize="18sp"       
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/ivImage"
            app:layout_goneMarginTop="32dp" />

        <TextView
            app:layout_goneMarginTop="32dp"
            android:id="@ id/tvDesc"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="14dp"
            android:layout_marginTop="12dp"
            android:layout_marginEnd="14dp"         
            android:textColor="@color/black"
            android:textSize="17sp"        
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tvTitle"          />

    </androidx.constraintlayout.widget.ConstraintLayout> 

Is it possible to implement the behavior as in the image using ConstraintLayout from layout (not programmatically).

Please, help me.

CodePudding user response:

you can do it programmatically :

  val layoutParams = imageView.layoutParams as RelativeLayout.LayoutParams
  layoutParams.setMargins(100, 100, 100, 100)

CodePudding user response:

I don't think there is a way to do what you ask just with XML and gone margins. You can do it programmatically like it is suggested, but you can also get the spacing you want by using a enter image description here

tvTitle gone*

enter image description here

ImageView and tvTitle gone

enter image description here

  •  Tags:  
  • Related