Apply Material Design (android:Theme.Material) to Android versions smaller than API 21?

Asked

Viewed 1,907 times

4

I was studying a little about the Android layout guidelines as:

So I tried to customize my Theme, thus:

<resources>
  <!-- inherit from the material theme -->
  <style name="AppTheme" parent="android:Theme.Material">
    <!-- Main theme colors -->
    <!--   your app branding color for the app bar -->
    <item name="android:colorPrimary">@color/primary</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="android:colorPrimaryDark">@color/primary_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="android:colorAccent">@color/accent</item>
  </style>
</resources>

But the attributes, android:colorPrimary, android:colorPrimaryDark and android:colorAccent, is only available for recent versions of Android (API 21+), which is currently not mine target.

So I took another route, to customize the components manually. So I tried to set the color of the Statusbar, with the method setStatusBarColor(int color), only this is also not available for versions prior to API 21.

Questions:

  1. Is there any way to set a solid color for Statusbar, for Android API 21- (Be it XML, or code)?
  2. Is there an Appcompat, which provides compatibility of the themes "Material" version smaller than API 21?
  3. There is some other way to get this result, for Android API 21-?

Important remarks

  • Man minTarget is API 14.
  • 1

    Have you read this part? http://developer.android.com/training/material/compatibility.html

  • @Lucassantos, worse than I had not seen. Hehe. I am creating a test project, to try to implement it. Thanks for the tip.

  • sure will work. I implemented in my project and it worked. You are welcome.

  • @Lucassantos, in my test project even worked, but still, did not change the color of the Statusbar as cited here, where the color of the Statusbar is to be the colorPrimaryDark, I’m running on an Android 4.2, and the Statusbar I’m still black, even try set with colorPrimaryDark in the style with blue. Is this the correct behavior? Note: Actionbar changed color as expected.

  • 3

    Fernando, the color of Statusbar is only changed with the attribute colorPrimaryDark on SDK 21, or using Tinted/Translucent Statusbar on Android 19. Out of this there is no way to change the color...

  • 2

    @Wakim, I get it, so actually the compatibility API doesn’t make this behavior consistent for all versions of Android, it only allows you to use it where it’s allowed, in this case, only in API 21+, and still keep it running without the Feature, for smaller versions. Right?

  • Exactly, coloring the Status Bar does not have compatibility at all, but other properties such as colorPrimary and accentColor have paper, but the colorPrimaryDark only in version 21+.

  • @Fernando I had a similar problem than yours. I had to change the color of the Ratingbar stars but it was only available for equal/higher versions than Lollipop. I ended up having to resort to libraries on Github to change the color and be compatible.

Show 3 more comments

1 answer

1

for versions smaller than api 21 you just create two Styles 1- for <21 and 2 for version >21, follow the example of how to implement in smaller versions.

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/primary</item>
    <!--   darker variant for the status bar and contextual app bars -->
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <!--   theme UI controls like checkboxes and text fields -->
    <item name="colorAccent">@color/accent</item>
</style>

  • 1

    I even understood your suggestion, but in this case it’s not quite what I want! What I would like when I asked this question was to maintain the same behavior between Android versions, implementing the themes of Material Design, and the compatibility library indicated by @Lucassantos, solved the problem missing only be able to color the Statusbar, in versions below API 21, which was found not to be possible.

Browser other questions tagged

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