Android databinding set padding if value is true

Store padding value in dimen.xml and use it. Please keep habit to write binding string with " " (double quotes)

android:paddingBottom="@{isGroupType ? @dimen/padding_normal : @dimen/padding_null}"

and so on for other paddings also.


For anyone looking to set margins via DataBinding, you'll have to use BindingAdapter as well:

@BindingAdapter("layoutMarginBottom")
fun setLayoutMarginBottom(view: View, dimen: Float) {
    val layoutParams = view.layoutParams as MarginLayoutParams
    layoutParams.bottomMargin = dimen.toInt()
    view.layoutParams = layoutParams
}

And your xml property will look like this:

app:layoutMarginBottom="@{someCondition ? @dimen/zero_dp : @dimen/twenty_dp}"

@Ravi's answer is correct.

But for more flexibility you can also try this:

@BindingAdapter({"padding", "shouldAdd"})
public static void setPadding(AppCompatImageView imageView, boolean shouldAdd, int padding){
    if (shouldAdd){
        imageView.setPadding(padding, padding, padding, padding);
    }
}

Then:

<android.support.v7.widget.AppCompatImageView
        android:layout_width="64dp"
        android:layout_height="64dp"
        shouldAdd="@{isGroupType}"
        padding="@{10}"/>

Just as a heads-up this does not work with layout_margin's :(

Not sure why, but think it's due to the parent layout needs to be remeasured..