macroid
macroid copied to clipboard
enhancement: better interop with theme attributes
this is a downside of moving away from xml, that the xml conveniences such as theme attributes become unavailable. I don't know a good way to handle this yet
I don’t think themes can be applied programmatically :( However I’ve never been able to use them effectively — personally I find the whole theme XML API (inheritance, composition, namespacing) very confusing. On the other hand, for many cases styling with tweaks works rather well.
Actually themes, is just mixed in attribute sets that is passed to views as constructor parameters. This could be done in the code, the process is not much different from supplying a bundle to the Intent, from code standpoint, although I'm not sure how you could convert existing theme.xml to such AttributeSet.
That I'm doing to bypass this limitation without much code (and due to my project is pretty big and complex, with huge legacy), is that I'm just using xml as usual, and then wire views using either Transformer and pattern-matching them (hackish, but works for me), or finding them by id.
AFAIK there is no way to create AttributeSet programmatically. See for example this SO answer.
Yeah, not using the LayoutBuilding DSL is another way to approach this :)
Looking at the documentation, this example exists:
XmlPullParser parser = resources.getXml(myResouce);
AttributeSet attributes = Xml.asAttributeSet(parser);
Given that XmlPullParser is used, that you could do (if you really wish to create themes from code) is create xml from code and so on... So, with proper wrapping around, this still can look like macroid's Bundle API.
Even more from docs: AttributeSet is actually just an Interface, so you could implement it as you wish, inspiring by looking at code of Views that are actually reading attributes out of it.
This is interesting — we could write an implementation of AttributeSet with +, etc — as an alternative to tweaks... I’ll try to poke with it when I have time.