Android Item Resource has a Name and Type
Here’s a handy trick to use instead of constants in your code. It uses the Item resource tag to define application specific constants which are located in a resource XML file.
For example, given the following XML located in a file in the res/values directory
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="host" type="setting">10.0.2.2</item> <item name="port" type="setting">3000</item> </resources>
I can now use the following Java syntax within an Android Activity:
String host = getString( R.setting.host ); int port = getResources().getInteger( R.setting.port );
This gives me a scheme to have a common place with ‘types’ to define constants. I’ve just barely started using it, but it seems like a nice clean way of handling these ‘magic’ values without using static final variables.
