Can't form a chain between two views/widgets in Android Studio
Asked Answered
B

16

42

When I'm using the layout editor in Android Studio and I try to make a chain (bidirectional constraint) between a EditText View and a Button View using the constraint anchors, it doesn't make the chain.

It only makes a constraint if I try to constrain one View to the other.

I am trying to chain the right side of the EditText to the left side of the Button.

This is what my Layout Editor looks like:

Layout Editor

Barny answered 13/3, 2017 at 6:40 Comment(2)
the android tutorial is terrible. they leave out so many steps, buttons aren't where they say they are, sequences don't work the way they say they should... .VERY frustrating. Someone needs to make a tutorial on how to take the tutorial. If anyone finds a better alternative please post.Embank
Same problem here... currently it seems you have to use the "center horizontally" option after selecting both views. Maybe it's the version?Followup
H
20

I was trying to figure this out too. I've discovered that one way to do it is to select both views, then right click and select Center Horizontally. This creates the chain, but then you have to adjust any other constraints accordingly. I'm new to Android, so I'm sure there will be other ways....

Hieratic answered 13/3, 2017 at 9:22 Comment(0)
K
12

I had the same issue. Solved it by going in into the XML as instructed by the tutorial: https://developer.android.com/training/basics/firstapp/building-ui.html

On the tutorial, click "See the final layout XML" and compare. My XML was missing:

app:layout_constraintLeft_toRightOf="@+id/editText"
Karynkaryo answered 19/3, 2017 at 21:36 Comment(0)
S
8

In Android Studio 3.0.1, choose the two objects (on the xml design tab) and right click on one of them and you will have under "chain" option two options: one to chain horizontally and second to chain vertically.

enter image description here

Sheepshanks answered 23/3, 2018 at 10:37 Comment(0)
H
3

I got a solution, probably will not be the best one until someone really answer properly, but works. I hope this helps others guys who stuck in the same place as me, so you can continuous the work.

Looks like the interface of android studio is not working properly when came to create chains. Some options from people here work for like 2 or 3 elements, but I has 5 elements.

So the answer is solve this in the code XML.

My steps are for Horizontal arrangement, if you wanna vertical just change Right/Left for Top/Bottom

I put all the elements in the place that I want and remove all connections. (Than in my case I connect the top and bottom so they can be in the middle.)

Then I connect the first element in the left and the last element in the right. And connect the right of each element in the left side of the next element.

app:layout_constraintRight_toLeftOf="@id/right_element"

Image of elements connect normal, no chain yet

After that I go inside the code and put manually the connection to the left element.

app:layout_constraintLeft_toRightOf="@+id/left_element"

And the chain was created. Image of elements connect with chain

I hope this help, sorry for don't post the pictures, I don't have reputation enough yet XD.

Hypophosphate answered 16/10, 2017 at 17:15 Comment(0)
C
2

i think android studio ui editor need more improvement for creating chain currently i am using Android Studio Preview 3.0 Canary 3

Sometime from editor it works perfectly but sometime it not, when linking not happen from ui editor we need to add constraint manually as per requirement Vertical or horizontal chain following are constraints

layout_constraintTop_toTopOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintBaseline_toBaselineOf
layout_constraintStart_toEndOf
layout_constraintStart_toStartOf
layout_constraintEnd_toStartOf
layout_constraintEnd_toEndOf

and also we declare chain style manually in XML as follow

layout_constraintHorizontal_chainStyle or layout_constraintVertical_chainStyle

CHAIN_SPREAD -- the elements will be spread out (default style)

Weighted chain -- in CHAIN_SPREAD mode, if some widgets are set to MATCH_CONSTRAINT, they will split the available space

CHAIN_SPREAD_INSIDE -- similar, but the endpoints of the chain will not be spread out

CHAIN_PACKED -- the elements of the chain will be packed together. The horizontal or vertical bias attribute of the child will then affect the positioning of the packed elements

hope android studio editor will improve this

Communicable answered 5/6, 2017 at 5:31 Comment(0)
F
2

I worked out that in Android Studio 3.2.2 you have to click on the views in the component tree, so left click first component and then holding shift click the second component and then right click and in the menu you will see chains as in my screenshot below.

Android Studio 3.2.2

enter image description here

Frech answered 15/4, 2019 at 15:22 Comment(0)
C
1

Resolved by adding constraints in both edit text (app:layout_constraintRight_toLeftOf="@+id/button") and button (app:layout_constraintBaseline_toBaselineOf="@+id/editText")

Complete example as below

<EditText
    android:id="@+id/editText"
    android:layout_width="245dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginStart="16dp"
    android:ems="10"
    app:layout_constraintRight_toLeftOf="@+id/button"
    android:hint="@string/edit_message"
    android:inputType="textPersonName"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginTop="16dp"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_marginRight="16dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBaseline_toBaselineOf="@+id/editText"
    app:layout_constraintLeft_toRightOf="@+id/editText"
    android:layout_marginLeft="16dp" />
Contrecoup answered 22/5, 2017 at 2:32 Comment(0)
A
1

The way I have been able to create a chain view in the blueprint layout is by drag click, select the objects to be linked. Then while they are selected right click and select "center horizontally" After doing this I can then create the other constraint and chain

Atp answered 6/6, 2017 at 0:22 Comment(0)
A
1

This can specifically be resolved by holding the shift button and pressing both of the widgets. After this process, you can then right-click one of the widgets to be able to chain the views. Use this for the EditText View as well as the button particularly.

Alpheus answered 28/6, 2021 at 18:51 Comment(0)
E
0

I solved this by creating the chain in blueprint mode. The tutorial never says you have to go back to it but if you do you can create the chain.

Elidaelidad answered 11/5, 2017 at 20:46 Comment(1)
How did you do it? I'm in the blueprint mode but but the anchors aren't connecting (not showing green)Sarabia
I
0

Just a tip when following the tutorial is to make sure that the Android Studio is upto date. I was wondering where certain buttons are when following the tutorials but found i was using an older version.

In terms of the question, the best is given by James @ 6/6/17.

  1. Add three buttons into the view
  2. Select them all and right click
  3. Select "Center Horizontally"

This does the trick to create the chain

Incalculable answered 22/6, 2017 at 23:53 Comment(0)
T
0

I disobeyed the tutorial by turning on Autoconnect (because I was trying everything).

I selected both widgets, then selected Center Horizontally. The chain was created, I turned Autoconnect back off, then continued the tutorial.

Taper answered 7/7, 2017 at 0:35 Comment(0)
T
0

The DESIGN tab is very prone to bugs! Just do what you'd want to do in the design but write it via XML. No need for tutorials it's self-explanatory, connect all the lefts and rights!

Tonsorial answered 22/10, 2017 at 17:2 Comment(0)
B
0

I had the same problem before, and from what I can I tell, we have the same problem.

The tutorial expects you to use android studio 3 version. When I got this problem, I realized that my android studio still 2.2.3. After installing the update for the version 3 and sdk etc to the latest version it worked.

Hope this helps.

Betray answered 3/11, 2017 at 7:44 Comment(0)
F
0

what caused the problem for me- i duplicated one of my views in my layout (to speed things up- or so i thought). by doing so - i caused the problem - several views had the same android:id value. which is a big no-no.

a unique value to all my view (by changing the android:id ) helped fix this problem

Fineberg answered 28/6, 2018 at 19:15 Comment(0)
E
0

I was having the same issue as well and what worked for me was selecting both views while holding the CMD button instead of the SHIFT button.

As a reference, I'm using the macOS version of Android Studio and was selecting both views using the SHIFT button which resulted in Chains being grayed-out.

I hope that helps somebody!

Eudocia answered 18/6, 2020 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.