Devin Dixon
Devin Dixon

Reputation: 12403

Android XML Dynamically Set Margin Heights

In android, I need to have layout sizes dependent on variables in the ViewModel. For example:

<TextView
                android:id="@+id/amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Example"
                android:layout_marginTop="@{viewModel.interfaceEnable ? 0dp : 10dp}"
                />

Based on the value in viewModel.interfaceEnable, its either going to be 0dp or 10dp. But the error I am getting is this:

Syntax error: extraneous input 'p' expecting {'.', '::', '[', '+', '-', '*', '/', '%', '<<', '>>>', '>>', '<=', '>=', '>', '<', 'instanceof', '==', '!=', '&', '^', '|', '&&', '||', '?', ':', '??'}

How can I set dp or any value through that kind of check?

Upvotes: 1

Views: 590

Answers (1)

Daniel Beleza
Daniel Beleza

Reputation: 419

You can do something like this:

android:layout_marginTop="@{viewModel.interfaceEnable ? @dimen/no_margin : @dimen/margin_normal}"

Upvotes: 1

Related Questions