Android databinding set padding if value is true

18,248

Solution 1

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.

Solution 2

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}"

Solution 3

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..

Solution 4

@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}"/>

Solution 5

@Ravi's answer is good, but it's working only for padding. If You want to simply add margin, create empty view e.g TextView with padding.

Share:
18,248

Related videos on Youtube

BillHaggerty
Author by

BillHaggerty

Software engineer and Computer scientist

Updated on June 26, 2022

Comments

  • BillHaggerty
    BillHaggerty almost 2 years

    I want to be able to to be able to set padding values if a boolean is true. The problem is that Android studio cannot parse the layout because it thinks 2dp is a decimal with a value of 2 and then doesn't know what to do with the p. how do I format this so that it understands i mean 2 density pixels.

    Data layout:

    <data class=".ItemBinding">
        <variable name="isGroupType" type="Boolean"/>
    </data>
    

    View layout(whats important):

    <android.support.v7.widget.AppCompatImageView
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:paddingBottom='@{isGroupType ? 2dp : 0dp}'
                android:paddingTop='@{isGroupType ? 8dp : 0dp}'
                android:paddingRight='@{isGroupType ? 2dp : 0dp}'
                android:paddingLeft='@{isGroupType ? 2dp : 0dp}'/>
    
    • BillHaggerty
      BillHaggerty almost 8 years
      Escaping the d doesn't help. Trying the second option.
  • user2137020
    user2137020 almost 7 years
    Why bindings should be written with " "? If you use ' ' than you can use " " inside binding.
  • Ravi
    Ravi almost 7 years
    That's vice versa thing, if you write it in between " ", you can also use ` ` inside it.
  • Matt Robertson
    Matt Robertson about 5 years
    Strange but true. I thought for sure I had something configured incorrectly. Thanks for the heads-up.
  • Yurets
    Yurets about 5 years
    it's because it can't parse dp. It expects numbers only, which would be pixels in that case
  • Paul Spiesberger
    Paul Spiesberger over 4 years
    This only works with paddings NOT with margins
  • Janusz Hain
    Janusz Hain over 4 years
    I would change to @BindingAdapter("android:layout_marginBottom") so it can be used with normal margin in layout
  • Sarthak Mittal
    Sarthak Mittal over 4 years
    @JanuszHain yup it can be done, I personally prefer separating data binding stuff from android namespace
  • mili
    mili about 3 years
    Accepted answer is not working for me, but this works fine. thanks for the complete answer. you saved me by putting both layout and java code. much thanks....

Related