1

I have a constraint app:layout_constraintStart_toStartOf="parent". Runtime I need to change this constraint to app:layout_constraintStart_toEndOf="@+id\myid" .

From my research i found only constraintSet.connect(viewid, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END, 50); . But how to achieve my requirements with this. Any idea on this?

V I J E S H
  • 7,464
  • 10
  • 29
  • 37

1 Answers1

1

This is in kotlin but it's barely different from Java.

  1. Give an ID to the rootLayout.
  2. Now, use this code:

    val constraintSet = ConstraintSet()
    constraintSet.clone(rootLayout) //Your rootLayout
    constraintSet.connect(
       yourView.id,                //ID of the view whose position you want to change
       ConstraintSet.START,      
       yourMyIdView.id,            //ID of the correspondent view
       ConstraintSet.END
    )
    constraintSet.applyTo(rootLayout) //Your rootLayout
    

ProTip : You can also animate the change by setting animateChange to true in rootLayout (XML) or you can use a Transition animation which I always prefer.

val transition: Transition = ChangeBounds()
transition.interpolator = AccelerateInterpolator() //Or Any other Interpolator
transition.duration = 700 //Duration
TransitionManager.beginDelayedTransition(rootLayout, transition) //Your rootLayout
constraintSet.applyTo(rootLayout) //Remember to put above 4 lines before this

You can further add Alpha animation it by using yourView.animate().alpha(1f).duration = 700 //1f (0 to 1) is the value and 700 is the duration.

Remember, it is barely different from Java, you just have to add ; at the end possibly.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50