Schnodahipfe
Schnodahipfe

Reputation: 1053

Android style descriptions

I tried changing the appearance of a spinner and I partly succeeded. I'm doing this via overriding parts of the theme. I managed to change the text size of the spinner item (i.e. the text size in the drop down button) with my themes.xml and styles.xml:

My themes.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="@android:Theme.Holo.Light">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:spinnerItemStyle">@style/CustomSpinnerItem</item>
</style>
</resources>

My styles.xml file looks like this:

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

<style name="CustomSpinnerItem" parent="@android:Widget.TextView.SpinnerItem">
    <item name="android:textAppearance">@style/CustomTextAppearance</item>
</style>
<style name="CustomTextAppearance">
    <item name="android:textSize">30dp</item>
</style>

</resources>

However I cannot find the attributes that are responsible for the text appearance of the items in the dropdown list of the spinner. I tried dropDownItemStyle amongst other things. In my opinion the attribute names are not self-explanatory, so I wanted to know whether there is a documentation of what attribute does what in a style to find out which attributes to override. I found it very cumbersome to trace back all the styles used in a theme via the themes.xml and styles.xml of the platfrom and then try to find the right attributes via trial and error.

I know that one can change the appearance by passing layouts to the adapter, however, this is not really what I was looking for, since (as far as I know), you can only use inheritance in styles and not in layout xml files. If I created a custom layout for the adapter I'd have to create 9-patch images etc., which I think is a bit too time consuming in case I only want to change the text size.

Of course it's possible that I misunderstood the whole concept, since I'm new to Android ;)

Upvotes: 1

Views: 519

Answers (1)

Julien Rousseau
Julien Rousseau

Reputation: 985

You probably have found out the answer since you asked but for others looking at similar questions:

I do not know of a list of attribute names with good explanation of what they do (R.attr's page mostly gives information that is already in the name) but the way I do it is:

  • Start from the element I give to setDropDownViewResource(), in my case: android.R.layout.simple_spinner_dropdown_item and find.

  • Find its layout definition in \sdk\platforms\android-17 (specific platform version to avoid redundant results).

  • Get its style from the layout file. In this case: ?android:attr/spinnerDropDownItemStyle

We now have the attribute name we need.

It's better to do it that way rather than try to guess what attribute to use because you know which attribute the system itself use so it's very likely to be the correct one (unless there's a bug).

If I created a custom layout for the adapter I'd have to create 9-patch images etc.

Well, no, the layout determines what kind of GUI element you would have (a textfield, a spinner, an imagebutton, a custom element...), not how they are styled (nine-patch backgrounds, text colors...), so you still would have to mess with styles to get the right appearance.

For example, for visual consistency I ported the button, checkbox and spinner style from Theme.Holo to Gingerbread, yet I did not mess with layout, all I did was the aforementioned steps plus looking up the result (spinnerDropDownItemStyle in the above example) in themes.xml, which gave me the style name (e.g.: Widget.Holo.DropDownItem.Spinner).

Then I looked that up in styles.xml and imported it (and any parent*) in my project's styles.xml, searching and copying any Holo specific reference in my project and adjusting the namespace accordingly (add android: to attributes and replace ?android:attr with @style for what I copy to my styles.xml file).

So far I haven't had to mess with layouts at all (even the presence of radio buttons in spinner dialogs on Gingerbread is determined by an xml attribute: android:checkMark).

  • If a style has no parent attribute (like Widget.Holo.DropDownItem.Spinner) then its parent is the same style minus the last element (e.g.: Widget.Holo.DropDownItem)

Upvotes: 1

Related Questions