xml - Can there be conditional statements on custom android attributes? -


most of know there possibility give custom attributes custom views in android. explained quite brilliantly example in this thread here on stackoverflow. question is:

is possible present such attributes upon fulfillment of condition?

what mean (pseudo-code):

<?xml version="1.0" encoding="utf-8"?> <resources>     <declare-styleable name="mycustomview">         <attr name="isclock" format="boolean" />     </declare-styleable>      <if name="isclock" value="true">         <attr name="timezone" format="string">     </if>     <else>         <attr name="somethingelse" format="string>     </else> </resources> 

now 1 possibility not have work "wrong" attributes in java-code, obviously:

public class mycustomview {     public mycustomview(context context) {          typedarray styleables = context.obtainstyledattributes(attrs, r.styleable.mycustomview);         boolean choice = styleables.getboolean(r.styleable.mycustomview_isclock, false);          if(choice) {             // it's clock, react other attrs         } else {             // don't react         }          styleables.recycle();     } } 

another way ilomambo suggested in answer: create various custom views different names , let them have attributes belong them.

but i'm asking myself if it's possible not confuse programmer of .xml-file in first place , offer him stuff needs combined in 1 place. after is in way done android (well... ide/lint/the parser...) when hinting example width or height of view should set 0dp when using layout_weight.

but if had guess i'd it's possible if rewrite android xml-parser... can please prove me wrong?

thanks in advance

if understand right, have 1 custom view can different attributes conditonal on third attribute.

to keep xml programmer aware of illegal attributes suggest 1 of 2 approaches:

  1. (simple approach) create 1 custom view each "name" , each own declare-styleable group.

    <resources>     <declare-styleable name="clockcustomview">         <attr . . . />     </declare-styleable>     <declare-styleable name="othercustomview">         <attr . . . />     </declare-styleable>     <!-- common attributes declared outside declare-styleable -->     <attr . . . /> </resources> 
  2. (more complete more complex) create xsd schema xml, way programmer can validate xml rules. xsd xml, have learn elements. see this link more information on xsd, there lots of info in web if google too.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -