Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this the correct way to have different values/styles for different versions

Tags:

android

Android Studio 2.1 preview 3

This is just a question, as I am confused as I have seen many alternatives in doing this.

I have created a new android project and my Activity extends AppCompatActivity

public class MainActivity extends AppCompatActivity {

I want to have the transparent statusbar on devices running 21 and over.

So in my values/styles I have the following

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

And in my values-21/styles I have the following

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!-- Make the statusbar transparent -->
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

My Manifest I select the theme

android:theme="@style/AppTheme"

Just some questions

  1. Is this the correct way, or is there any better way to do this?
  2. Would values-21/styles inherit all the colors in values/styles so I would have to repeat this?
like image 616
ant2009 Avatar asked Mar 20 '16 10:03

ant2009


People also ask

How to change theme of app in android?

In the Project pane select Android, go to app > res > values > themes > themes.

What is theme on android?

Android Themes A theme is nothing but an Android style applied to an entire Activity or application, rather than an individual View. Thus, when a style is applied as a theme, every View in the Activity or application will apply each style property that it supports.

How to use theme in android studio?

Go to File > Settings, now under IDE settings click on appearance and select the theme of your choice from the dropdown. File > Import Settings , select the file or your choice and select ok a pop up to restart the studio will open up click yes and studio will restart and your theme will be applied.


2 Answers

It's the right way. May I suggest you to organize your style better?

values/styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="CommonTheme">

    </style>

    <style name="CommonTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

values-v21/styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="CommonTheme">
        <!-- All customization of the theme for this version -->
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

So you don't need to repeat the common values of the style for every api level.

like image 118
Mimmo Grottoli Avatar answered Oct 18 '22 23:10

Mimmo Grottoli


I will try to answer it giving some references

Maintaining Compatibility

To avoid duplication of code, define your styles inside res/values/, modify the styles in res/values-v21/ for the new APIs, and use style inheritance, defining base styles in res/values/ and inheriting from those in res/values-v21/

So you should try to avoid code duplication in your style.xml at different folders res/values/ and res/values-v21/ by using style inheritance.

Style Inheritence

If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period.

If you want to inherit a style that you've defined yourself you can skip adding a parent attribute and inherit from it using a dot or period notation.

With this, you can define a base theme BaseTheme in res/values/ with different colors and inherit from it as BaseTheme.StyledStatusBar without specifying a parent attribute.

<resources>
    <style name="BaseTheme.StyledStatusBar"></style>

    <!-- Base application theme. -->
    <style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

In values-21/, add item android:windowTranslucentStatus to BaseTheme.StyledStatusBar

<resources>
    <style name="BaseTheme.StyledStatusBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>

In manifest, select the theme

android:theme="@style/BaseTheme.StyledStatusBar"
like image 13
random Avatar answered Oct 19 '22 00:10

random