Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintLayout with databinding

Is it possible to do something like this:

Xml:

        <android.support.constraint.Guideline
             app:layout_constraintGuide_percent="@{viewModel.guidelinePercent}"
        />

ViewModel:

  @Bindable
    public float getGuidelinePercent() {
        return condition ? 0.6f : 0.8f;
    }

I'm getting this error:

Cannot find the setter for attribute 'app:layout_constraintGuide_percent' with parameter type float on android.support.constraint.Guideline.

I've tried with BindingAdaptor but it doesn't change the value:

ViewModel:

@BindingAdapter(value = {"constraintPercent"})
public static void setConstraintPercent(Guideline view, float percent){
    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.setGuidelinePercent(view.getId(), percent);
}

Xml:

<android.support.constraint.Guideline
                 app:constraintPercent="@{viewModel.guidelinePercent}"
            />

Any thoughts?
Thanks.

like image 639
GuilhE Avatar asked Jun 30 '17 11:06

GuilhE


People also ask

Can I use both Data Binding and ViewBinding?

There is nothing ViewBinding can do that DataBinding cannot but it costs longer build times. Remember you don't need to use both, if you are using DataBinding, there is no need adding ViewBinding.

What is the difference between ConstraintLayout and Linearlayout?

ConstraintLayout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout). Despite the fact that it's awesome, it fails to serve the purpose with simple UI layouts.

Is ConstraintLayout a ViewGroup?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).


2 Answers

This issue may be related to Android Databinding cannot set the layout_width (layout_height) property. In any case, add the following code to ViewModel:

public float getGuidelinePercent() {
    return 0.5f; // Of course, this default could be anything you want.
}

@BindingAdapter("layout_constraintGuide_begin")
public static void setLayoutConstraintGuideBegin(Guideline guideline, float percent) {
    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
    params.guidePercent = percent;
    guideline.setLayoutParams(params);
}

Your XML for the Guideline will look something like this:

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="@{viewModel.guidelinePercent}"/>

btw, thanks for this idea. I have been wanting an easier way to adjust guidelines programmatically.

like image 57
Cheticamp Avatar answered Oct 16 '22 15:10

Cheticamp


The ConstraintSet you're creating should be set to the ConstraintLayout sometime after setting the percentile.

@BindingAdapter(value = {"constraintPercent"})
public static void setConstraintPercent(Guideline view, float percent){
    ConstraintLayout constraintLayout = (ConstraintLayout) view.getParent();
    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);
    constraintSet.setGuidelinePercent(view.getId(), percent);
    constraintLayout.setConstraintSet(constraintSet);
}
like image 28
tynn Avatar answered Oct 16 '22 15:10

tynn