Programmatic access to themes / styles / attrs in android

I would like to access complex resources ("total resources") compiled into my apk. For example, getting all the attributes of the current topic, preferably as xml, I can cross.

Themes / styles can be obtained using getStyledAttributes (), but this requires knowledge of the attributes in advance. Is there a way to get a list of attributes that exist in a style?

For example, in a topic like this:

<style name="BrowserTheme" parent="@android:Theme.Black"> <item name="android:windowBackground">@color/white</item> <item name="android:colorBackground">#FFFFFFFF</item> <item name="android:windowNoTitle">true</item> <item name="android:windowContentOverlay">@null</item> </style> 

How can I access the elements without knowing their names in advance?

Another example would be attrs.xml, where some attributes have enumerations or flags, for example:

 <attr name="configChanges"> <flag name="mcc" value="0x00000001" /> <flag name="mnc" value="0x00000002" /> ... </attr> 

How can an application receive these flags without knowing their name?

+6
source share
2 answers

Instead of Theme.obtainStyledAttributes(...) , Resources.obtainTypedArray(int) can be used to access all style attributes, without specifying any attributes that interest you.

You can then access the TypedArray elements to find the resource identifier / types / values ​​for each attribute.

 TypedArray array = getResources().obtainTypedArray( R.style.NameOfStyle); for (int i = 0; i < array.length(); ++i) { TypedValue value = new TypedValue(); array.getValue(i, value); int id = value.resourceId; switch (value.type) { case TypedValue.TYPE_INT_COLOR_ARGB4: // process color. break; // handle other types } } 
+3
source

There is probably a better way, but you can always access the "raw" XML using getXml

+1
source

Source: https://habr.com/ru/post/902716/


All Articles