S
S
Sergey Vodakov2016-09-20 21:13:54
Java
Sergey Vodakov, 2016-09-20 21:13:54

Android. How to organize the choice of styles in the application?

A banal thought was born: to make the application the ability to change styles. Those. the user selected in the menu, from the list "Orange" - once, and everything became in shades of orange. I chose "Blue" - it became blue. 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's cumbersome. And the question was born in my mind, is it possible to fill in this menu with a list of styles somehow differently, in a loop?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SofroN, 2016-09-21
@WaterSmith

Make an array with styles and strings
and loop add

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <array name="styleNames">
        <item>@string/AppThemeBlackName</item>
        <item>@string/AppThemeBrownName</item>
        <item>@string/AppThemeAmberName</item>
    </array>
<!-- можно написать так -->
    <string-array name="styleNames1">
        <item>Черный</item>
        <item>Коричневый</item>
        <item>Оранжевый</item>
    </string-array>

    <integer-array name="styleIDs">
        <item>@style/AppThemeBlack</item>
        <item>@style/AppThemeBrown</item>
        <item>@style/AppThemeAmber</item>
    </integer-array>

</resources>

Get array from code
String[] names = getResources().getStringArray(R.array.styleNames);
int[] ids= getResources().getIntArray(R.array.styleIDs);

with an integer array, I'm not sure that it will work like that, now there is nothing to check on.
But I think you understand the direction where to dig

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question