1

I have a simple layout of horizontally chained views with spread_inside chain style. When I try to move the views using the bias attribute to desired position I've found out that bias attribute is ignored.

Here is layout for reference:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <View
        android:id="@+id/view_1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@android:color/black"
        app:layout_constraintEnd_toStartOf="@id/view_2"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view_2"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:background="@android:color/black"
        app:layout_constraintEnd_toStartOf="@id/view_3"
        app:layout_constraintStart_toEndOf="@id/view_1"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view_3"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@android:color/black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.2"
        app:layout_constraintStart_toEndOf="@id/view_2"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Here I would like to move the view_3 closer to view_2 using layout_constraintHorizontal_bias attribute. How can I achieve that?

cheshie
  • 421
  • 1
  • 6
  • 18

2 Answers2

5

Horizontal biasing does not work if you are using a horizontal chain among your views (as you're giving biasing in the same axis, vertical biasing would work if there is a horizontal chain and vice-versa); except for the case if you want to apply it on your created chain's head view (which is the left-most view in a horizontal chain and the top-most view in a vertical chain); which is not your case, here. Also, the applied biasing on the head view of the chain only works if the selected chain style is packed. So, you should try and find some other workaround to achieve your desired UI and ignore the use of chain (here).

For more information, refer: Build a Responsive UI with ConstraintLayout

I hope my answer helps you.

Nitin Gurbani
  • 1,220
  • 6
  • 16
3

You can change first view (chain head view) like this:

 <View
    android:id="@+id/view_1"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@android:color/black"
    app:layout_constraintEnd_toStartOf="@+id/view_2"

    app:layout_constraintHorizontal_bias="0.1"
    app:layout_constraintHorizontal_chainStyle="packed"

    app:layout_constraintStart_toStartOf="parent" />

and set all chain position relative to parent via layout_constraintHorizontal_bias, but inside chain this doesn't work.

igor_rb
  • 1,821
  • 1
  • 19
  • 34
  • 2
    understood. but why is that? why horizontal bias won't work with horizontal chains? – cheshie Sep 26 '18 at 14:49
  • Because in a chain if you give biasing to one of its view then the biasing of the view(s) connected to that view automatically changes as well (for views which are connected in the same axis in which the biasing is being given or set) since they are all bidirectionally connected to each other; thereby causing a lot of ambiguity of their actual positioning as well as their bias values. That's why there are only certain types supported which can be applied when forming a chain in Android to show up all the variations in which chains can be used or they are applicable. – Nitin Gurbani Sep 26 '18 at 15:30