Themes, Styles and support android Library

Asked

Viewed 278 times

5

Hello since I started working with Android development, I have always had a hard time understanding how the part of Themes, styles and libs of support Android, I always have a lot of difficulty for example to stylize a EditText or a RatingBar. For example now little was styling a RatingBar where I had in my theme the following item:

<style name="CustomActionBarTheme"
    parent="Theme.AppCompat">
    <item name="android:ratingBarStyle">@style/customRatingBar</item>

<style name="customRatingBar" parent="android:Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/ratingbar_full_holo_dark</item>
    <item name="android:indeterminateDrawable">@drawable/ratingbar_full_holo_dark</item>
</style>

In API 23 it worked smoothly, but for it to work on smaller APIS I had to put the following code in the style-v11

  <style name="CustomActionBarTheme"  parent="Theme.AppCompat">
    <item name="android:ratingBarStyle">@style/customRatingBar</item>
    <item name="ratingBarStyle">@style/customRatingBar</item>
</style>

What I want to understand is, what’s the difference between android: and without the android: in the item?

1 answer

4


These attributes are only available from API level 21. In order to be used in previous versions we will have to resort to those that are defined in appCompat api.

So that these Styles/themes is applied either the application runs on pre devices or runs on post devices API level 21 you must define them twice, once using the prefix android: and one without the prefix.

The prefix android: refers to the name of the package where the style is defined and should be used for the case of API level 21(or higher, than compileSdkVersion refers). Not using the prefix is referring to Styles/themes as defined in appCompat api.

The procedure to be followed is as follows::

  • Set a theme inherited from Theme.AppCompat(1) in res/values/styles.xml.
  • Define a theme with the same name inherited from the Material theme in res/values-v21/styles.xml.
  • Set this theme as the theme of your application in the manifest file.
  • In the res/values/styles.xml don’t use the prefix android: when referring to attributes only existing after the API level 21.
  • Utilize res/values-v21/styles.xml to define Styles/themes that nay are compatible with the pre-versions API level 21, always use the prefix android:

For more information see documentation.

(1) After Appcompat v21.1.1 you must inherit from Theme.AppCompat.Light.NoActionBar or Theme.AppCompat.Dark.NoActionBar and you’ll have to use Toolbar instead of Actionbar

Browser other questions tagged

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