Doubt with style files (Styles.xml) on Android

Asked

Viewed 1,035 times

1

On Android it is possible to make multiple style files, one for each version of the level of API (16, 19, 21...), for each type of screen (xxxdp) and etc. But a question arose: there is some kind of inheritance among these files?

For example: I am working on a project where the minimum level of API is 16 (JellyBean). For this, I have 3 style files: one for the API 16, one for the API 19 and one for the API 21. However, within these files, I am defining the same thing (well to say) 3 times:

API 21:

    <style name="AppTheme.Base" parent="android:Theme.Material.Light.NoActionBar.TranslucentDecor">
        <item name="">@color/blue_500</item>
        <item name="">@color/blue_700</item>
        <item name="">@color/amber_a700</item>
        <!--<item name="">true</item>-->
        <item name="alertDialogProTheme">@style/Theme.AlertDialogPro.Material.Light</item>
    </style>

    <style name="DarkToolbar" parent="AppTheme.Base">
        <item name="">@color/abc_primary_text_material_dark</item>
        <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item>
        <item name="">@color/abc_primary_text_material_dark</item>
    </style>
</resources>

API 19:

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

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/blue_500</item>
        <item name="colorPrimaryDark">@color/blue_700</item>
        <item name="colorAccent">@color/amber_a700</item>
        <item name="">true</item>
        <!--<item name="">true</item>-->
        <item name="alertDialogProTheme">@style/Theme.AlertDialogPro.Material.Light</item>
    </style>

    <style name="DarkToolbar" parent="AppTheme.Base">
        <item name="">@color/abc_primary_text_material_dark</item>
        <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item>
        <item name="">@color/abc_primary_text_material_dark</item>
    </style>
</resources>

API 16:

<resources>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/blue_500</item>
        <item name="colorPrimaryDark">@color/blue_700</item>
        <item name="colorAccent">@color/amber_a700</item>
        <!--<item name="">true</item>-->
        <item name="alertDialogProTheme">@style/Theme.AlertDialogPro.Material.Light</item>
    </style>

    <style name="DarkToolbar" parent="AppTheme.Base">
        <item name="">@color/abc_primary_text_material_dark</item>
        <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item>
        <item name="">/abc_primary_text_material_dark</item>
    </style>
</resources>

My question is: if I set only once the colors used in colorPrimary, colorPrimaryDark and colorAccent, as well as defining only once the style DarkToolbar, i need to reset them for each summer of API, in other words, reassign the values for each version of Android that my app supports?

Editing: when trying to run the application using the above Xmls, I received an error stating that I should use the "Appcompat" theme instead of the "Material" theme, but the phone I’m testing is API 21 (Android 5.0), should support the Material theme. Any hint of what might be?

1 answer

1


I believe that if all their styles derive from AppCompat you can use a standard attribute to be used in all styles. Below I show you a very simple example.

strings.xml (API: 21)

<resources>
    <string name="text_info">Minha API é 21!!!</string>
</resources>

strings.xml (API: 19)

<resources>
    <string name="text_info">Minha API é 19+!!!</string>
</resources>

strings.xml (API: Standard)

<resources>
    <string name="text_info">Minha API não é 19 e nem 21</string>
    <string name="text_info2">Esse texto vai ser mostrado em todas as APIS!</strings>
</resources>

Above, you can see that each XML has a string with different content. However, in XML standard, we have a text_info2 which does not appear in the other XML. When you show this text, the system will check if the settings for API 21 have this text, if it does not have it, it will display the default text. And... back to the subject: yes, when you put a default value in your style setting, if there is no configuration for your current API, the system will use the value of the API pattern.

  • Thank you, array! That was exactly my question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.