The official documentation of the constraint set connect says: https://developer.android.com/reference/android/support/constraint/ConstraintSet.html#connect(int, int, int, int, int)
void connect (int startID,
int startSide,
int endID,
int endSide,
int margin)
the margin to constrain (margin must be postive)
For my understanding if I want to connect two views with the left to right of then this margin is left margin.
//left to right of
constraintset.connect(textView.id,ConstraintSet.LEFT,previousTextViewId,ConstraintSet.RIGHT,10)
then 10 is the left margin. Am I right? I have implemented this concept but no margin is set even no right or left. What am I missing?
Update: It looks like this issue has been fixed, but I haven't checked it out. See the bug report.
Your understanding is also how I understand things. Here is a quick way to check how things are working.
In the layout below, the top left corner of textRight
lines up with the lower right corner of textLeft
. When MainActivity
runs, textRight
should move 1,000px down and 1,000px to the right. It moves 1,000 px down but does not move to the right at all.
I believe that this is an outstanding issue. See this issue report.
I don't know a work-around and I am surprised that this could even be a bug. I am willing to stand corrected if anyone sees the error.
two_text_views.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="@+id/textLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" />
<TextView
android:id="@+id/textRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView2"
android:layout_marginTop="0dp"
app:layout_constraintTop_toBottomOf="@+id/textLeft"
app:layout_constraintStart_toEndOf="@+id/textLeft"
android:layout_marginStart="0dp" />
</android.support.constraint.ConstraintLayout>
**MainActivity.java**
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
ConstraintLayout layout;
super.onCreate(savedInstanceState);
setContentView(R.layout.two_text_views);
ConstraintSet constraints = new ConstraintSet();
constraints.clone(layout);
constraints.connect(R.id.textRight, ConstraintSet.LEFT, R.id.textLeft, ConstraintSet.RIGHT, 1000);
constraints.connect(R.id.textRight, ConstraintSet.TOP, R.id.textLeft, ConstraintSet.BOTTOM, 1000);
constraints.applyTo(layout);
}
}
EDIT So, here is a fix. Use ConstraintSet.START
and ConstraintSet.END
instead of ConstraintSet.LEFT
and ConstraintSet.RIGHT
. I just tried it and it works OK. I can't say why left and right don't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With