How i can change button marginEnd with LiveData and BindingAdapter?
My LiveData in ViewModel:
var marginRight = MutableLiveData(R.dimen.default_margin)
My dimen
<dimen name="default_margin">141dp</dimen>
and my attribute: android:layout_marginEnd="@{viewmodel.marginRight}"
In this case i get
Cannot find a setter for <android.widget.Button android:layout_marginEnd> that accepts parameter type 'androidx.lifecycle.MutableLiveData<java.lang.Integer>'
If a binding adapter provides the setter, check that the adapter is annotated correctly and that the parameter type matches.
after searching the Internet, I realized that there is no setter for the margin. It helped me:
LiveData:
var marginRight = MutableLiveData(R.dimen.margin_right_default)
My BindingAdapter:
@JvmStatic
@BindingAdapter("app:marginRight")
fun setMarginRight(view: View, marginResource: Int){
val resources = view.context.resources
val params = view.layoutParams as ViewGroup.MarginLayoutParams
params.setMargins(0,0, resources.getDimension(marginResource).toInt(), 0)
view.layoutParams = params
}
and my attribute:
app:marginRight="@{viewmodel.marginRight}"