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.
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.
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.
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).
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With