Velixo Velixo - 4 months ago 20
Android Question

Load value from dimens.xml and make it negative

In an xml document, is there any way to load a value from dimens.xml and make it negative?
For example, if I have:

<dimen name="x">20dp</dimen>


Is it possible to do something like this:

android:layout_marginRight="-@dimen/x"


So I in de facto get:

android:layout_marginRight="-20dp"

Answer

That is not possible in standard layout XML. You could retrieve the dimension value in Java, multiply it by -1, and apply it in Java.

You could even do that in the form of a custom attribute (e.g., yourapp:layout_negativeMarginRight) in your own custom subclass of ViewGroup, if you really wanted. This would seem to be overkill.


UPDATE: This is now somewhat possible via data binding expressions:

android:padding="@{0.75f * @dimen/icon}"

It does not work for margins at the moment due to this bug.

Comments