Velixo Velixo - 3 months ago 12x
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:


So I in de facto get:



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.