A trivial thought was born: make it possible to change styles in the application. Those. the user chose from the menu, from the list "Orange" - once, and everything became in shades of orange. Chose "Blue" - it became blue. He created several styles with different coloring. And the question arose how to organize the filling of the menu? It turned out like this:

menu.add(R.id.themesMenuGroup, R.style.AppThemeBlack, 0, R.string.AppThemeBlackName); menu.add(R.id.themesMenuGroup, R.style.AppThemeBrown, 0, R.string.AppThemeBrownName); menu.add(R.id.themesMenuGroup, R.style.AppThemeAmber, 0, R.string.AppThemeAmberName); // ------- еще много аналогичных строк выброшено для экономии места ----------- 

But somehow it is cumbersome. And the question was born to me, is it possible to somehow fill this menu with a list of styles?

  • y UI components you can dynamically change styles, what is the question then? Can't figure out the menu? - Shwarz Andrei
  • No, the question is how to fill the menu in a loop, so as not to write 15 lines of adding menu items. And for this you need to somehow go around all the styles, and they are registered in the class R.style as static fields. I think that it is necessary to do this through reflection. But he doubted, suddenly there is a more native way. - Sergey Vodakov September
  • drop everything into an array, R.style ... this is a regular int, and run through the loop. to reflex the menu and to climb in natice it is not necessary)) suddenly everything will break. - Shwarz Andrei
  • I apologize for stupidity, throw in an array of how? Stupidly listing 15 resources? These are the same eggs. I’m only going to bake it for the beauty of the code, but so that when a new style is added, it will appear on the menu itself. - Sergey Vodakov September

1 answer 1

Through reflection all the same happened. Created a class that provides access to styles for selection and fills the menu passed to it. Criticize please.

 public class StyleHelper { private static StyleHelper instance; private ArrayList<ContentValues> styleValues; private StyleHelper(){ } private void init(){ styleValues = new ArrayList<>(); Field[] styleFields = R.style.class.getDeclaredFields(); for (int i=0; i<styleFields.length; i++){ Field styleField = styleFields[i]; String styleName = styleField.getName(); if (styleName.startsWith("AppTheme")){ try { ContentValues styleValue = new ContentValues(); styleValue.put("style",styleFields[i].getInt(styleName)); styleValue.put("name",R.string.class.getField(styleName+"Name").getInt(styleName+"Name")); styleValues.add(styleValue); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } } } public void fillMenu(Menu menu, int groupId){ for (ContentValues styleValue: styleValues) { menu.add(groupId,styleValue.getAsInteger("style"),0,styleValue.getAsInteger("name")); } } public static StyleHelper getInstance() { if (instance==null){ instance = new StyleHelper(); instance.init(); } return instance; } 

}