I have an Android application, and i want to store some generic view attributes (primarily width and height) in a separate XML file (values.xml). An example:
<Button
android:id="@+id/button1"
android:layout_width="@strings/genericbuttonwidth"
android:layout_height="wrap_content"
android:text="Button" />
with the res/values/strings.xml looking like:
<resources>
<string name="genericbuttonwidth">50dp</string>
</resources>
The Eclipse graphical layout viewer makes the width 50dp, as given in strings.xml, but when ran on an Android (physical) device, it throws an exception:
Caused by: java.lang.RuntimeException: Binary XML file line #174: You must supply a layout_width attribute.
So what I am trying to do doesn’t really work (when I replace the the reference with 50dp, it works nicely). Any way or workaround to load the attributes from a separate XML file?
XML file saved at res/values/dimens.xml:
This application code retrieves a dimension:
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
This layout XML applies dimensions to attributes: