Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing enumerated style attributes from XML in Android

Tags:

android

I have an app that requires scrollbars to be enabled in one landscape orientation but not in portrait orientation. I thought the easiest way to do this would be to make an attribute indicating whether the scrollbar is enabled or not, such as the following:

<ScrollView
        a:layout_width="fill_parent"
        a:layout_height="fill_parent"
        a:scrollbars="?my_activity_scrollbars"
        a:fadingEdge="none"
        >

I would then define separate values of the attribute for landscape and portrait modes. Seemed easy.

So I then defined this attribute in attrs.xml:

<resources>
  <declare-styleable name="Theme">
    <attr name="my_activity_scrollbars" format="enum" /> 
  </declare-styleable>
</resources>

And added it to my app's styles.xml:

<style name="MyApp" parent="@android:style/Theme.Light">
    <item name="add_credit_card_scrollbars">none</item> 
</style>

However, when I try to compile my app, I get the following error:

styles.xml:8: error: Error: String types not allowed (at 'my_activity_scrollbars' with value 'none').

It seems clear that it's treating "none" as a string rather than an enumerated value. Rather than using "none" I tried things like "?android:attr/scrollbars/none", "?android:attr/none", etc, but those didn't work.

How can I specify the "none" value as an enumerated value instead of a string?

like image 969
emmby Avatar asked Jul 01 '10 19:07

emmby


1 Answers

I believe you have your attr definition set incorrectly:

This is an attr enum from the android source:

<attr name="ellipsize">
    <enum name="none" value="0" />
    <enum name="start" value="1" />
    <enum name="middle" value="2" />
    <enum name="end" value="3" />
    <enum name="marquee" value="4" />
</attr>

It appears you do not define format="enum" and instead specify the enum values within the attr tag itself.

like image 197
Rich Schuler Avatar answered Nov 11 '22 11:11

Rich Schuler