Home » Android » xml – Style hierarchy in Android – what is the order of importance?

xml – Style hierarchy in Android – what is the order of importance?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have an android application that I’m working on with a custom theme applied to it in the android manifest on the main activity itself. This activity creates a listview, which has the style applied to it.

If I create a custom layout for that listview, including a separate xml file for the rows themselves, and I apply styling directly to those layouts, does this style overwrite the overall style for the application? I’m trying to get a grasp on the hierarchy of events as far as how styling and themeing works.

The way I’m GUESSING it works in my example is: apply style for row, referenced by row layout xml > apply style for overall listview, referenced by listview layout xml > apply style from custom theme, referenced by style xml referenced by android manifest

Am I right? Or am I approaching this incorrectly. (just for confusions sake, by “>” I mean has a greater importance than)

How to&Answers:

If you’ve specified the same attributes in multiple places, the list below determines which attributes are ultimately applied. The list is ordered from highest precedence to lowest:

  1. Applying character- or paragraph-level styling via text spans to TextView-derived classes
  2. Applying attributes programmatically
  3. Applying individual attributes directly to a View
  4. Applying a style to a View
  5. Default styling
  6. Applying a theme to a collection of Views, an activity, or your entire app
  7. Applying certain View-specific styling, such as setting a TextAppearance on a TextView

Answer:

Hope I am understand your question right here…

The styles you define in styles.xml will always overwrite the styles coming from the theme currently used by android.

But this only works for the attributes you overwrite.

If you leave an attribute untouched, android will provide the style for it, and sometimes this comes bite you in the butt 🙂

This system is best described like this:
A textview requires an attribute example

<item name="android:textColor">#00FF00</item>

Android will first look in the original layout.
If not found, it will look into your custom styles.
If not found, it will look into android styles.

Hope this helps.

Answer:

The standard themes have lines like which define the ListView style:

    <item name="listViewStyle">@android:style/Widget.ListView</item>

In your own theme you can do a

    <item name="listViewStyle">@style/MyOwnListView</item>

Something that is not defined in the ListView style (own or default) will be what is defined in the theme if you have defined it there.