com.androidplot.util
Class Configurator

java.lang.Object
  extended by com.androidplot.util.Configurator

public abstract class Configurator
extends Object

Utility class for "configuring" objects via XML config files. Supports the following field types: String Enum int float boolean

Config files should be stored in /res/xml. Given the XML configuration /res/xml/myConfig.xml, one can apply the configuration to an Object instance as follows:

MyObject obj = new MyObject(); Configurator.configure(obj, R.xml.myConfig);

WHAT IT DOES: Given a series of parameters stored in an XML file, Configurator iterates through each parameter, using the name as a map to the field within a given object. For example:

 <config car.engine.sparkPlug.condition="poor"/>
 
 

Given a Car instance car and assuming the method setCondition(String) exists within the SparkPlug class, Configurator does the following:

 car.getEngine().getSparkPlug().setCondition("poor");
 
 

Now let's pretend that setCondition takes an instance of the Condition enum as it's argument. Configurator then does the following:

car.getEngine().getSparkPlug().setCondition(Condition.valueOf("poor");

Now let's look at how ints are handled. Given the following xml:

would result in: car.getEngine.setMiles(Integer.ParseInt("100000");

That's pretty straight forward. But colors are expressed as ints too in Android but can be defined using hex values or even names of colors. When Configurator attempts to parse a parameter for a method that it knows takes an int as it's argument, Configurator will first attempt to parse the parameter as a color. Only after this attempt fails will Configurator resort to Integer.ParseInt. So:

would result in: car.getHood().getPaint().setColor(Color.parseColor("Red");

Next lets talk about float. Floats can appear in XML a few different ways in Android, especially when it comes to defining dimensions:

Configurator will correctly parse each of these into their corresponding real pixel value expressed as a float.

One last thing to keep in mind when using Configurator: Values for Strings and ints can be assigned to localized values, allowing a cleaner solution for those developing apps to run on multiple form factors or in multiple languages:


Constructor Summary
Configurator()
           
 
Method Summary
static void configure(android.content.Context ctx, Object obj, HashMap<String,String> params)
           
static void configure(android.content.Context ctx, Object obj, int xmlFileId)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Configurator

public Configurator()
Method Detail

configure

public static void configure(android.content.Context ctx,
                             Object obj,
                             int xmlFileId)
Parameters:
ctx -
obj -
xmlFileId - ID of the XML config file within /res/xml

configure

public static void configure(android.content.Context ctx,
                             Object obj,
                             HashMap<String,String> params)


Copyright © 2010-2013 androidplot.com. All Rights Reserved.