5 Replies Latest reply on Nov 29, 2007 11:29 AM by AlvinNguyen

    How can I set button colors from xml file data?

    AlvinNguyen
      I am reading an xml file to create a group of buttons. I am also using repeater to create the group of buttons.

      I am getting the buttons created, but am unable to set the fill color properly. I keep getting the error "Implicit coercion of a value of type String to an unrelated type Array" if I try to plug in r.currentItem.value, even when I used an Actionscript function to convert from a String to an array of 2 uints. Is there a simple type casting solution to this I'm just not getting?

      Any help would be greatly appreciated!

      The xml file has the following format:

      <colors>
      <color>
      <name>Canteloupe</name>
      <value>0xFFCC66</value>
      </color>
      <color>
      <name>Banana</name>
      <value>0xFFFF66</value>
      </color>
      <color>
      <name>Lemon</name>
      <value>0xFFFF00</value>
      </color>
      <color>
      <name>Honeydew</name>
      <value>0xCCFF66</value>
      </color>
      </colors>

      The code I have so far is:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="colors.send()" layout="absolute">
      <mx:HTTPService id="colors" url="assets/colors_standard.xml" resultFormat="e4x"/>
      <mx:XMLListCollection id="xlc_colors" source="{colors.lastResult.color}"/>
      <mx:TabNavigator left="0" top="0" right="0" bottom="0">
      <mx:Canvas label="Tab 1" width="100%" height="100%">
      <mx:Accordion width="200" left="0" top="0" bottom="0">
      <mx:Canvas label="Colors" width="100%" height="100%">
      <mx:Label text="Current Color Selection" left="10" right="10" top="10" height="20" fontWeight="bold"/>
      <mx:Button label="Update Colors" left="10" right="10" top="227" height="22" id="button_colors_update"/>
      <mx:Canvas height="189" left="10" right="10" top="30" borderStyle="outset" backgroundColor="#e9e9e9">
      <mx:Tile id="tile_standard" direction="horizontal" tileWidth="25" tileHeight="25" horizontalAlign="center" verticalAlign="middle" borderStyle="outset" left="10" right="10" top="10" height="134" backgroundColor="#ffffff">
      <mx:Repeater id="r" dataProvider="{xlc_colors}">
      <mx:Button toolTip="{r.currentItem.name}" height="15" width="15" x="10" y="10"/>
      </mx:Repeater>
      </mx:Tile>
      <mx:Button label="Additional Colors" id="button_colors_online" height="22" left="10" right="10" bottom="10"/>
      </mx:Canvas>
      </mx:Canvas>
      </mx:Accordion>
      </mx:Canvas>
      </mx:TabNavigator>
      </mx:Application>

        • 1. Re: How can I set button colors from xml file data?
          atta707 Level 2
          fillColor style accepts an array. for a first step this should work:

          <mx:Button toolTip="{r.currentItem.name}" height="15" width="15" x="10" y="10"
          fillColor="[red, red]"/>
          • 2. Re: How can I set button colors from xml file data?
            AlvinNguyen Level 1
            Thanks atta707!

            If I send an array of 2 uints via a function call that converts from String to Number to uint in an Actionscript function and try to feed that into fillColors via:

            <mx:Script>
            <![CDATA[
            public function fillcolors (_colorvalue:String):uint {
            return uint(Number(_colorvalue));
            }
            ]]>
            </mx:Script>

            ...
            <mx:Button height="15" width="15" toolTip="{r.currentItem.name}" x="10" y="10" fillColors="[fillcolors({r.currentItem.value}),fillcolors({r.currentItem.value})]"


            I still get the "Implicit coercion of a value of type String to an unrelated type Array." on the line that creates the button. I have tried returning an Array of two uints with the Actionscript funtion and get the same result.

            <mx:Script>
            <![CDATA[
            public function fillcolors (_colorvalue:String):Array {
            return new Array(uint(Number(_colorvalue)), uint(Number(_colorvalue)));
            }
            ]]>
            </mx:Script>
            ...
            <mx:Button toolTip="{r.currentItem.name}" height="15" width="15" x="10" y="10" fillColors="[fillcolors({r.currentItem.value})]"/>


            Am I mistaken in my conversion of r.currentItem.value to fillColors as arguments? Sorry about the newbie question - the error I get makes me think it is a type conversion issue, but I can't seem to get this to work/compile. If fillColors is expecting an Array, should the Actionscript function returning an Array at least get this to compile?
            • 3. Re: How can I set button colors from xml file data?
              atta707 Level 2
              Here you go:

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
              verticalAlign="middle" horizontalAlign="center"
              layout="horizontal">

              <mx:Script>
              <![CDATA[

              [Bindable]
              private var array:Array = ['blue', 'red'];
              private var _color:String = "red";

              private function fillColors(c:String) : Array {
              // StyleManager.getColorName('red'), StyleManager.getColorName('blue')
              return [c, c];
              }
              ]]>
              </mx:Script>

              <mx:Repeater id="rp" dataProvider="{array}">
              <mx:Button fillColors="{fillColors(String(rp.currentItem))}" label="Test Button"/>
              </mx:Repeater>
              </mx:Application>
              • 4. Re: How can I set button colors from xml file data?
                ChiefRocka00
                thanks atta, I was struggling to figure this out as well a couple of days ago