Here's my story:
I got a custom ViewGroup that I want to create from code using a predefined style, my approach so far has been creating an AttributeSet object from a style.xml element, like so (warning, beware of the copy-paste code ahead):
XmlPullParser parser = getResources().getXml(R.style.my_stylez);
AttributeSet attributes = Xml.asAttributeSet(parser);
But when doing so I get some crazy error: "..android.content.res.Resources$NotFoundException: Resource ID #0x7f090002 type #0x12 is not valid"
I'm know I'm probably missing something very obvious here (or am I?), and would be grateful if any of you guys can point me in the right direction.
Thanks
You need to start with a resource identifier for an XML file, preferably in res/xml. Then you can obtain an AttributeSet by first creating an XmlPullParser:
Resources res = context.getResources();
XmlPullParser parser = res.getXml(R.xml.some_xml_file);
// Seek to the first tag.
int type = 0;
while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
type = parser.next();
}
// Wrap as an attribute set.
AttributeSet attrs = Xml.asAttributeSet(parser);
You can find examples of this in the drawable CTS tests in AOSP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With