Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding layout constraints

I have problems to add an end constraint to view

 <android.support.constraint.ConstraintLayout
    android:id="@+id/chatDocumentMessageContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@{chatMessage.corespondent==Corespondent.Sent? @drawable/very_rounded_corners_gray_background:@drawable/very_rounded_corners_orange_background}"
    android:maxWidth="300dp"
    android:minWidth="140dp"
    android:onClick="@{clickListener::onClick}"
    android:padding="@dimen/padding_large"
    app:layout_constraintEnd_toEndOf="@{chatMessage.corespondent==Corespondent.Sent? ConstraintSet.PARENT_ID:ConstraintSet.UNSET}">

I got this error:

[kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'app:layout_constraintEnd_toEndOf' with parameter type int on android.support.constraint.ConstraintLayout.

Tryed adding the following adapter, but still not working:

 @BindingAdapter(" app:layout_constraintEnd_toEndOf")
    fun setEndConstraint(guideline: Guideline, resource: Int) {
        val params = guideline.getLayoutParams() as ConstraintLayout.LayoutParams
        params.endToEnd = resource
        guideline.layoutParams = params
    }
like image 286
Victor Radulescu Avatar asked Jan 28 '23 02:01

Victor Radulescu


1 Answers

I created a BindingAdapter for this kind of a situation:

@BindingAdapter(
    "layout_conditionalConstraint_startSide",
    "layout_conditionalConstraint_toEndId",
    "layout_conditionalConstraint_endSide",
    "layout_conditionalConstraint_condition"
)
fun setConditionalConstraint(
    view: View, startSide: Int, endId: Int, endSide: Int, condition: Boolean
) {
    val constraintLayout = (view.parent as? ConstraintLayout) ?: return
    with(ConstraintSet()) {
        clone(constraintLayout)
        if (condition) connect(view.id, startSide, endId, endSide)
        else clear(view.id, startSide)
        applyTo(constraintLayout)
    }
}

In your case you could use it like this:

<android.support.constraint.ConstraintLayout
    android:id="@+id/chatDocumentMessageContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@{chatMessage.corespondent == Corespondent.Sent ? @drawable/very_rounded_corners_gray_background : @drawable/very_rounded_corners_orange_background}"
    android:maxWidth="300dp"
    android:minWidth="140dp"
    android:onClick="@{clickListener::onClick}"
    android:padding="@dimen/padding_large"
    app:layout_conditionalConstraint_startSide="@{ConstraintSet.END}"
    app:layout_conditionalConstraint_toEndId="@{ConstraintSet.PARENT_ID}"
    app:layout_conditionalConstraint_endSide="@{ConstraintSet.END}"
    app:layout_conditionalConstraint_condition="@{chatMessage.corespondent == Corespondent.Sent}">

But in most cases you should be able to get the desired layout with the features the ConstraintLayout offers, this BindingAdapter is only useful in a handful of corner cases or complex layouts.

like image 81
janosch Avatar answered Jan 31 '23 18:01

janosch