I've got a custom component that I'm instantiating in a loop, and they all need to have the same style applied. As it's a custom component (specifically a subclass of LinearLayout), I can't just set the style attribute in the XML, because the root tag of the component's layout XML is a merge (to save making the view hierarchy too crazy). I found this:
How can I create an AttributeSet from a style.xml?
Which describes my problem pretty much exactly. Only the answers are useless - my R.java is fresh, and the style is there. I think the problem is that getResources().getXml(R.style.my_style) doesn't actually look in the /res/values/styles.xml file?
I'm using Scala, so the syntax is a little...nicer. My component:
class MyLinearLayout(object : MyDatabaseObject, context : Context, attrs : AttributeSet = null)
extends LinearLayout(context, attrs) {
// Shows and interacts with the data in object
}
In the Activity:
val componentStyle = Xml.asAttributeSet(getResources.getXml(R.style.my_style))
for (obj <- dbResults) viewGroup.addView(new MyLinearLayout(obj, this, componentStyle))
And the first line of that throws a Resources.NotFoundException. The style itself is in res/values/styles.xml, along with the rest of them, and gets applied to LinearLayouts in different activities, so that's not the problem.