Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the android LayoutInflater use my Custom Resources

I have defined a custom class:

public class CustomResource extends Resources {}

in order to load custom external colors at runtime. This is working well in the Java code when using getResources().getColor().

When loading an XML layout via setContentView(), which contains references to colors, then getColor() of the CustomResources is never called when inflating XML resources.

I already overrode getResources and getSystemService in the context but getResources().getColor() simply does not get called by the View constructors when they resolve XML attributes.

It's because obtainStyledAttributes() from Resource.Theme is called from within the constructors of Widgets.

I now have no idea what I have to additionally overwrite.

Example: layout.xml contains a reference to drawable/button.xml, button.xml contains a reference to colors/somecolor.

I now need the call from - for example - a TextView constructor (the inflater creates a TextView by calling the constructor with the xml attributes as parameters) which then calls obtainStyledAttributes() to use my resources class for resolving colors rather than the TypedArray magic that is currently happening in obtainStyledAttributes().

like image 419
Xnyle Avatar asked Sep 18 '25 03:09

Xnyle


1 Answers

Yes. I have code for this here.

https://github.com/slightfoot/android-edge-effect-override/blob/master/EdgeEffectOverride.java

like image 180
Simon Avatar answered Sep 19 '25 19:09

Simon