My company's app supports changing the theme dynamically at runtime without having to restart the current Activity. It does this by walking the View tree and applying styles to each View that is configured to support it. The issue I'm running into, however, is that the drop down Views that are displayed for Spinners are never found when walking the View tree. This is the code for finding each View from the Activity:
void applyTheme(final int resourceId) {
setTheme(resourceId);
// Apply the theme to the activity's view.
styleView(findViewById(android.R.id.content));
// Apply the theme to any dialog fragments.
final List<Fragment> fragments = getSupportFragmentManager().getFragments();
for (final Fragment fragment : fragments) {
if (fragment instanceof DialogFragment) {
final Dialog dialog = ((DialogFragment)fragment).getDialog();
if (dialog != null) {
final Window window = dialog.getWindow();
if (window != null) {
styleView(window.getDecorView());
}
}
}
}
}
void styleView(final View view) {
// Apply styling here.
...
// Recursively find all children and apply the theme to them.
if (view instanceof ViewGroup) {
final ViewGroup viewGroup = (ViewGroup)view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
styleView(viewGroup.getChildAt(i));
}
}
}
The drop down views are not found no matter if the parent Spinner is defined in the Activity, a Fragment, or a DialogFragment. If the drop down Views are visible to the user, is there any way to actually retrieve references to them?