Color for drawable

Former Member
Former Member
<drawable-list id="BgFill" x="0" y="0" foreground="Gfx.COLOR_LT_GRAY">
<shape type="rectangle" width="fill" height="fill" />
</drawable-list>


How I can set color from code like findDrawableById("BgFill").setColor(myColor);?
  • I'd have to write a test to verify, but I believe you could...

    var bgfill = findDrawableById("BgFill");
    bgfill[0].setColor(myColor);


    It might be better for you to use a custom drawable for the background...

    class Background extends Ui.Drawable
    {
    hidden var _M_color;

    function initialize(params) {
    Drawable.initialize(params);
    _M_color = params[:color];
    if (_M_color == null) {
    _M_color = Gfx.COLOR_BLACK;
    }
    }

    function draw(dc) {
    dc.setColor(_M_color, _M_color);
    dc.clear();
    }

    function setColor(color) {
    _M_color = color;
    }

    function getColor() {
    return _M_color;
    }
    }


    Then in your layout...

    <layout id="MyLayout">
    <drawable id="BgFill" class="Background">
    <param name="color">Gfx.COLOR_RED</param>
    </drawable>

    <!-- other drawables go here -->
    </layout>


    Travis
  • Former Member
    Former Member over 8 years ago
    UnexpectedTypeException: Expected Symbol, given Number
  • As I said, I hadn't tested. I built a simple testcase. Given the resource definition...

    <drawable-list id="BgFill" x="0" y="0" foreground="Gfx.COLOR_LT_GRAY">
    <shape type="rectangle" width="fill" height="fill" />
    </drawable-list>


    You can compile with debugging output and see that the resource compiler is generating a class declaration like this...

    class BgFill extends Ui.Drawable
    {
    function initialize(params) {
    Drawable.initialize();
    }

    function draw(dc) {
    var pX = 0;
    var pY = 0;
    var pWidth = dc.getWidth();
    var pHeight = dc.getHeight();
    drawComplex(dc,pX,pY,pWidth,pHeight);
    }

    function drawComplex(dc, pX, pY, pWidth, pHeight) {
    dc.setColor(Gfx.COLOR_LT_GRAY,Gfx.COLOR_TRANSPARENT);
    dc.fillRectangle(pX,pY,pWidth,pHeight);
    }
    }


    So, there is nothing you can do to access the color used for the background. So, again, I suggest using a drawable of your own that you can control the color of.

    Travis
  • Former Member
    Former Member over 8 years ago
    Thank you it is work!