-1

I'm only starting my adventure with Android so please excuse if the question is silly, but I've been looking for an answer long and hard to no avail. The question is simple: can I programmatically alter the value of this parameter?

app:layout_constraintTop_toBottomOf="@+id/divider1"

What I'm trying to do is rather simple: I have a visible row of objects with a divider underneath, and a TextView below that I've constrained to that divider's bottom. I also have a second row of objects with another divider underneath that starts with visibility set to GONE but can be toggled visible with a floating action button. I need the TextView below the first row to move lower to make room for the second row when it appears. I can use setY, but considering that the two rows are the same, just with different values, it would be way easier to simply change the top constraint of the TextView from "divider1" to "divider2". Can it be done? Here's what the button does at the moment:

addButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!isPair2Visible) {
                    driverLabel2.setVisibility(View.VISIBLE);
                    followerLabel2.setVisibility(View.VISIBLE);
                    driver2.setVisibility(View.VISIBLE);
                    follower2.setVisibility(View.VISIBLE);
                    ratioLabel2.setVisibility(View.VISIBLE);
                    ratio2.setVisibility(View.VISIBLE);
                    divider2.setVisibility(View.VISIBLE);
                    isPair2Visible = true;
                }
            }
        });

And this is the TextView that I need to move:

    <TextView
        android:id="@+id/totalRatio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="@string/ratio_default"
        android:textColor="@color/colorAccentOrange"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="@+id/ratio1"
        app:layout_constraintTop_toBottomOf="@+id/divider1" />

And everything would be perfect if I could alter the last line to:

app:layout_constraintTop_toBottomOf="@+id/divider2" />

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 2
    The answer in this [question](https://stackoverflow.com/questions/45263159/constraintlayout-change-constraints-programmatically) should give you what you need. In the future please search for similar questions on stackoverflow before posting one. – Ayazmon Nov 27 '19 at 11:50
  • I did search for similar questions. And they all use solutions that are marked as deprecated by Android Studio documentation, and I was unable to get them to work in Android Studio 3.5.2. Trust me, I've tested a dozen solutions that used ConstraintSet and I just can't get it to work. – Paweł Kmieć Nov 27 '19 at 11:53

3 Answers3

1

Another option:

ConstraintLayout.LayoutParams textViewParams =
            (ConstraintLayout.LayoutParams) textView.getLayoutParams();
textViewParams.topToBottom = R.id.divider2;
textView.setLayoutParams(textViewParams);
guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • You are welcome. Other options mentioned the ConstraintSet... It is useful to change several views at once. However, since you are trying to change the anchor of a single view, I think this is the simplest way. – guipivoto Nov 27 '19 at 12:19
0

You can use ConstraintSet to do it:

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout); // parent layout of your TextView
constraintSet.connect(R.id.divider2, ConstraintSet.BOTTOM, R.id.totalRatio,ConstraintSet.TOP, 0);
constraintSet.applyTo(constraintLayout);
Yang Liu
  • 1,300
  • 10
  • 14
0

Try use ConstrainSet

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(your_constraint_layout);
constraintSet.connect(R.id.totalRatio,ConstraintSet.TOP,R.id.divider1,ConstraintSet.TOP,YOUR_DESIRE_MARGIN);
constraintSet.applyTo(your_constraint_layout);
Evyatar Cohen
  • 292
  • 1
  • 9
  • 25