How to sum values in xml?

android-layout, xml

Solution

You cannot perform arithmetic or logical operations in xml file. Dynamically you can retrieve the data from the xml and sum the values in java code, then set that value as parameters to your LinearLayout or FrameLayout or any other widget.

Problem

Suppose I have 2 dimension values defined in an XML file and want to define a 3rd value that is the sum of the other 2 values. Is there a way to do that in the XML file? Here is an illustration of what I would like to do. ``` <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="top_element_size">10dp</dimen> <dimen name="element_spacing">5dp</dimen> <dimen name="bottom_element_top">@dimen/top_element_size + @dimen/element_spacing</dimen> </resources> ``` And I would like to define `android:layout_marginTop="@dimen/bottom_element_top"` for the bottom element in a `frame` layout. But it appears that `@dimen/top_element_size + @dimen/element_spacing` is not legal.

Original source