I have the Activity added below, in which I try to create my own custom xml string,
Which from I want to parse a ColorStateList object
public class MainActivity extends Activity {
final String colorVals = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<selector xmlns:android=\"http://schemas.android.com/apk/res/android\">" +
"<item android:color=\"#333\" android:state_focused=\"true\" />" +
"<item android:color=\"#fff\" android:state_pressed=\"true\" />" +
"<item android:color=\"#666\" android:state_enabled=\"false\" />" +
"<item android:color=\"#222\"/>" +
"</selector>";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
String xml = colorVals;
InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
parser.setInput(is, "UTF-8");
Resources res = getResources();
ColorStateList colors = ColorStateList.createFromXml(res, parser);
TextView tv = new TextView(this);
tv.setTextColor(colors);
} catch (Exception e) {
Log.e("test", e.getMessage());
}
}
}
when trying to do so I get the next error:
10-22 16:23:04.006: E/test(16127): START_TAG (empty) http://schemas.android.com/apk/res/android}android:color='#333' {http://schemas.android.com/apk/res/android}android:state_focused='true'>@1:166 in java.io.InputStreamReader@41dcc110: tag requires a 'android:color' attribute.
Debugging the parser that I have created, I see in the attribute set:
attr[i] ="Android",
attr[i+1]="color",
I think I might be instantiating the parser wrong