title: Safe enum custom attributes in Android
body: <p>I have a situation that I presume is pretty common.  I have a Java enum:</p>
  
  <pre><code>public enum Flavour { CHOCOLATE, STRAWBERRY }
  </code></pre>
  
  <p>I have a custom attribute defined in attrs.xml:</p>
  
  <pre><code>&lt;attr name="flavour"&gt;
      &lt;enum name="chocolate" value="0" /&gt;
      &lt;enum name="strawberry" value="1" /&gt;
  &lt;/attr&gt;
  </code></pre>
  
  <p>But this feels really fragile.  It relies on me manually mapping from the attribute to the enum correctly.</p>
  
  <p>If someone adds "SARDINE" to the end of enum Flavour then it obviously won't be automatically added to the attribute definition.  That's fair enough.</p>
  
  <p>But worse, if someone adds "SARDINE" in the middle of the enum, it will break xml layouts that use "strawberry".</p>
  
  <p>How do people overcome this?  I've considered using string (and using <code>Flavour.valueOf()</code>), but I was hoping there might be a cleaner solution out there.</p>