Search code examples
androidattrandroid-custom-view

Mutually exclusive syled attributes for a custom view?


I have 2 attributes (amongst many others) in a styleable in attrs.xml.

Given these two:

 <attr name="enableScrolling" format="boolean"/>
 <attr name="showPadlock" format="boolean"/>

Is there any way to make them mutually exclusive? The design is for a generic scale to display alongside a graph. The specific use case for an instance of the generic scale is that if scrolling is enabled, the scale displays a padlock icon which toggles between locked and unlocked and disables/enables the scrolling at runtime.

Showing the padlock is not meaningful if enableScrolling is false. I deal with this in code but it would be much cleaner if I could somehow convey the semantics in XML.

I'm guessing that the answer is no since there is no UX paradigm in the IDE to deal with it but I welcome any thoughts.

Thanks.


Solution

  • You have 3 ways:

    • just ignore showPadlock if it has no sense
    • check attributes in code. Show warning message or throw an exception if you notice invalid combination.
    • create enum attr. For instance:

      <attr name="scrollType">

      <enum name="simpleScroll" value="1" />

      <enum name="scrollWithPadlock" value="2" />

      <enum name="none" value="3" />

      </attr>