findDrawableById but for bitmaps

Hi, I'm trying to find a bitmap ID using a string, something similar to what findDrawableById does, but for bitmaps. Is there a way?

Thanks

  • So this works

    var id = View.findDrawableById("label1");
    System.println("Answer " + id);
    

    when using this layout.xml

    <layout id="MainLayout">
        <label id="label1" x="center" y="10" text="@Strings.AppName" font="Graphics.FONT_MEDIUM" color="Graphics.COLOR_BLUE" justification="Graphics.TEXT_JUSTIFY_CENTER" />
        <label id="label2"  x="center" y="center" text="@Strings.Prompt" font="Graphics.FONT_SMALL" color="Graphics.COLOR_WHITE" justification="Graphics.TEXT_JUSTIFY_CENTER" />
    </layout>

    But this returns null for id

    var id = View.findDrawableById("LauncherIcon");
    System.println("Answer " + id);
    

    When using this drawable.xml

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

    I need to select a bitmap using this snippet of code which must load a bitmap by id, but it fails to find the Id and returns null.

    var icon_string = "frunk0trunk0port0_icon_white";
    var icon_array = icon_string.toCharArray();
     
    if (_data._vehicle_data.get("vehicle_state").get("ft") == 1) {
    	icon_array[5] = '1';
    }
    
    if (_data._vehicle_data.get("vehicle_state").get("rt") == 1) {
    	icon_array[11] = '1';
    }
    
    if (_data._vehicle_data.get("charge_state").get("charge_port_door_open") == true) {
    	icon_array[16] = '1';
    }
    
    icon_string = StringUtil.charArrayToString(icon_array);
    var icon_id = findDrawableById(icon_string);
    

    So I ended doing this abomination because I can't get findDrawableById to work for bitmaps :-<

    var which_bitmap = 0;
    
    if (_data._vehicle_data.get("vehicle_state").get("ft") != 0) {
    	which_bitmap = 1;
    }
    
    if (_data._vehicle_data.get("vehicle_state").get("rt") != 0) {
    	which_bitmap += 2;
    }
    
    if (_data._vehicle_data.get("charge_state").get("charge_port_door_open") == true) {
    	which_bitmap += 4;
    }
    
    switch (which_bitmap) {
    	case 0:
    		dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(Rez.Drawables.frunk0trunk0port0vent0_icon_white));
    		break;
    	case 1:
    		dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(Rez.Drawables.frunk1trunk0port0vent0_icon_white));
    		break;
    	case 2:
    		dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(Rez.Drawables.frunk0trunk1port0vent0_icon_white));
    		break;
    	case 3:
    		dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(Rez.Drawables.frunk1trunk1port0vent0_icon_white));
    		break;
    	case 4:
    		dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(Rez.Drawables.frunk0trunk0port1vent0_icon_white));
    		break;
    	case 5:
    		dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(Rez.Drawables.frunk1trunk0port1vent0_icon_white));
    		break;
    	case 6:
    		dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(Rez.Drawables.frunk0trunk1port1vent0_icon_white));
    		break;
    	case 7:
    		dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(Rez.Drawables.frunk1trunk1port1vent0_icon_white));
    		break;
    }
    

  • I think there's a confusion between a drawable resource and a layout element.

    A drawable resource is... just that. A resource. It has a reference ("Rez.Drawables.SymbolNameForYourDrawable") that you can use to load the resource, but the reference is just a reference with no properties until you have loaded it.

    Whereas...

    A layout element can be found inside a layout because it is a coded element of that layout.

    That said...

    You could simplify / streamline your code a lot by using the symbol in a list or something like that.

    EG: (And this is just as an example... not recommending the code style etc... just food for thought.)

    var iconList = [Rez.Drawables.frunk0trunk0port0vent0_icon_white,
    Rez.Drawables.frunk1trunk0port0vent0_icon_white,
    Rez.Drawables.frunk0trunk1port0vent0_icon_white,
    Rez.Drawables.frunk1trunk1port0vent0_icon_white,
    Rez.Drawables.frunk0trunk0port1vent0_icon_white,
    Rez.Drawables.frunk1trunk0port1vent0_icon_white,
    Rez.Drawables.frunk0trunk1port1vent0_icon_white,
    Rez.Drawables.frunk1trunk1port1vent0_icon_white];
    
    var which_bitmap = 0;
    
    if (_data._vehicle_data.get("vehicle_state").get("ft") != 0) {
    	which_bitmap = 1;
    }
    
    if (_data._vehicle_data.get("vehicle_state").get("rt") != 0) {
    	which_bitmap += 2;
    }
    
    if (_data._vehicle_data.get("charge_state").get("charge_port_door_open") == true) {
    	which_bitmap += 4;
    }
    
    dc.drawBitmap(image_x_left,image_y_top,Ui.loadResource(iconList[which_bitmap]);
    
    

  • Thanks. I'll give this a try and I think the confusion on my side is they called it findDrawableById and that's exactly the file drawables.xml is called lol.

  • https://developer.garmin.com/connect-iq/core-topics/layouts/

    The layout element in xml specifies an Array of drawable objects. The View.findDrawableById() searches this Array.

  • Ok, so basically I could add the bitmaps to the layout as resources, all of them at the same coordinate and select which one to use? This is the different bitmaps. Each one represent if something is opened or closed that's what that section of the screen displays