Cannot set constraint bias

sziraqui Source

I want to set app:layout_constraintHorizontal_bias="0.0" so that my TextView stays towards left. When I set it from layout editor by dragging the tiny circle to 0, the circle moves back to default 50. Also tried doing it from xml, but the layout has no effect on bias attribute at all. If I remove the right constraint, the text goes offscreen owing to wrap_content width. Adding right constraint (along with existing left constraint) requires me to set horizontal bias to achieve permanent left alignment

This answer suggests to add constraint to "parent" (instead of another view, I guess), I tried it and set left and right constraints to parent but still no luck. (left was constraint to image view)

I tried left aligning it with another view (by selecting notification_subject and notification_body and using "Align Left Edges"), still no change in layout

This is my TextView

<TextView
            android:id="@+id/notification_subject_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/notification_subject"
            android:textAppearance="@style/TitleTextAppearance"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintLeft_toRightOf="@id/notification_item_iv"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginRight="16dp"
            android:layout_marginLeft="16dp"
            app:layout_constraintHorizontal_bias="0.0"/>

Rendered layout in editor: enter image description here

androidandroid-studioandroid-constraintlayout

Answers

answered 1 week ago RhysP #1

I think you have two issues here:

  1. You are trying to match the constraint by using layout_width="0dp". I believe this should be set to layout_width="wrap_content".
  2. On one side of your constraints you are using layout_constraintEnd_toEndOf="parent" and then layout_constraintLeft_toRightOf="@id/notification_item_iv" on the opposite side. Please can you change this to layout_constraintStart_toEndOf="@id/notification_item_iv"

Complete code:

<TextView
        android:id="@+id/notification_subject_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/notification_subject"
        android:textAppearance="@style/TitleTextAppearance"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/notification_item_iv"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        app:layout_constraintHorizontal_bias="0.0"/>

comments powered by Disqus