You can do it with a custom Binding Adapter.
Create Binding Adapter as below
public class DataBindingAdapter {
@BindingAdapter("android:layout_width")
public static void setWidth(View view, boolean isMatchParent) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = isMatchParent ? MATCH_PARENT : WRAP_CONTENT;
view.setLayoutParams(params);
}
}
Then use this attribute in your View.
<TextView
android:layout_width="@{item.isMatchParent, default = wrap_content}"
...
/>
Note:
default = wrap_content is important, because width and height should be specified when view is created, whether binding happens after bit time of view rendering.
Explaination
Why it is not possible without BindingAdapter.
Because Android does not provide View.setWidth(), size can be set by class LayoutParams, so you have to use LayoutParams. You can not use LayoutParams in xml because again here is no View.setWidth() method.
That's why below syntax gives error
Cannot find the setter for attribute 'android:layout_width' with parameter type int
<data>
<import type="android.view.ViewGroup.LayoutParams"/>
<data>
android:layout_width="@{item.matchParent ? LayoutParams.MATCH_PARENT : LayoutParams.WRAP_CONTENT}"
Setting Visibility by xml works, because there is View.setVisibility() method available
Below syntax works
<data>
<import type="android.view.View"/>
<variable
name="sale"
type="java.lang.Boolean"/>
</data>
<FrameLayout android:visibility="@{sale ? View.GONE : View.VISIBLE}"/>