How to load Rez resource dynamically?

Hi, please can anyone help me how to load dynamically resource?

Normally I would do something like this: animation = new WatchUi.AnimationLayer(Rez.Drawables.resourcxy, {:locX=>0, :locY=>0});

But I would like to pass the resource ID (Rez.Drawables.resourcxy) as an input parameter (e.g. from my database) and then load it based on that.

I tried something like this: View.findDrawableById("TimeLabel") , but it does not work (I guess I call it incorrectly, or this is not even working for my solutions).

Thanks!

Top Replies

All Replies

  • View.findDrawableById is used to find drawables present in the active layout.

    As far as I know it isn't possible to dynamically load the resource by means of a parameter.
    The way I do it is with if-else or switch statements.

  • I must not understand the question.

    A symbol is just an object type in the MonkeyC type system. You should be able to pass them around as you see fit. For example, this should work:

    function dynamically_load_animation_layer(resourceId)
    {
      return new WatchUi.AnimationLayer(resourceId, {:locX=>0, :locY=>0})
    }

    That should work just fine.

    Provided you don't try to save symbols to application storage, you should be fine. Saving a symbol to app storage is a bad idea because the symbol id can change between compiles.

  • I think what the OP is trying to do, based on the fact that I would like to be able to do the same, is load a resource based on a setting or config or whatever that is coming from a database via API call. So, for example, if a user has their favourite colour set as "RED" that the app will load Rez.Drawables.Red, perhaps by constructing the string "Rez.Drawables.Red" and passing that in to the loadResource() method. However, as  pointed out, the only way to do this is to use if-else or switch statements.

  • perhaps by constructing the string "Rez.Drawables.Red" and passing that in to the loadResource() method

    Yes. It is not possible to read a Symbol directly from an external source, so at some level you will have to map from an external value (setting/String//Number) to a Symbol.

  • at some level you will have to map from an external value (setting/String//Number) to a Symbol

    and how do you do that? is it working also for images? I can not make it work

    imageID = "id_Yellow";
    image = dynamically_load(:imageID);
    with 
    function dynamically_load(resourceId)
    {
      return WatchUi.loadResource(resourceId);
    }
  • no, if you have resource

    <drawables>
        <bitmap id="LauncherIcon" filename="launcher_icon.png" />
    </drawables>

    you load it

    image = dynamically_load(Rez.Drawables.LauncherIcon);
  • But i found out that 

        imageID = [Rez.Drawables.id_ms_Yellow];
        image = WatchUi.loadResource(imageID[0]);

        image = WatchUi.loadResource( Rez.Drawables.id_ms_Yellow );
    gives the same results
    So i just need a way to create a "Symbol" (i guess) from a string, so that one is put together I can use it dynamically. In the resource I will put a single image with different id for different palette colors , so then it will be loaded the one chosen by settings and put on the screen. 
    something like from View: 

    string = "Yellow"
    symbol will be "Rez.Drawables.id_ms_"+string, so Rez.Drawables.id_ms_Yellow
    the conversion should get me the result without having to write all in once. 
    Hope you can help me 
    PS what I want is to print the same image png in different color depending on the settings
  • why array?

    imageID = Rez.Drawables.id_ms_Yellow;

    image = WatchUi.loadResource(imageID);

    or simply

    image = WatchUi.loadResource(Rez.Drawables.id_ms_Yellow);

    imageID is the symbol generated by compiler, it is the number but can change next after each compilation; in this case imageID is a member of hidden class (everything is changed into symbols)

    unfortunately you can't generate symbol from string 

    see 

    https://developer.garmin.com/connect-iq/reference-guides/monkey-c-reference/#symbols

    PS - You want to change background colour of png? Or rather one known colour from png to other? Find a proper function if is any

    developer.garmin.com/.../Graphics.html

    but probably you have to put a few pngs

    var r= Rez.Drawables, ri;

    if(sett==1)

    {

    ri=r.yellow;

    }else if(...)

    {

    ...

    }

    var  image = WatchUi.loadResource(ri);

  • string = "Yellow"
    symbol will be "Rez.Drawables.id_ms_"+string, so Rez.Drawables.id_ms_Yellow

    As _psx_ pointed out, you can't generate a symbol from a string. You can store a symbol in a variable, and use it for an indirect lookup using square brackets. e.g. If y is a symbol variable, you can look up y in object x with x[y];

    So the closest you can get to what you're looking for is something like this:

    // Array which maps integers to symbols of image resources in Rez.Drawables
    var imageSymbols = [
      :image_yellow, // 0
      :image_red, // 1
      :image_blue, // 2
      ...
    ];
    
    var imageColor = ... // read enumerated value from somewhere (0 = yellow, 1 = red, etc.)
    
    var image = WatchUi.loadResource(Rez.Drawables[imageSymbols[imageColor]]);

  • Are you sure it will run?

    is :image_yellow points to Rez.Drawables.image_yellow?