How to draw line in DataField

Hi all,

I need to draw a simple vertical line in the middle of a DataField I am building. 

How can I do that?
Is something I need to define in drawables.xml and/or layouts.xml files? 
Or is something you have to do directly in Monkey C code with DC object? 

I tried different solution I've found here on the Garmin Forum but nothing seems to work. 

Thanks for helping me. 

  • Tried with that code but is not working and I don’t know why!

  • If you are using layouts and call View.onUpdate(), if you want to use dc calls (like drawLine) they must be done after the View.onUpdate call.  If you do the dc calls before the View.onUpdate, they are actually drawn but the View.OnUpdate wipes them out.

    You can do fairly complex things with this, but you want to make sure you don't draw too many.  Heres a DF I did that shows a breadCrumb trail, where I limit the number of data points used to draw it.  I have the same thing in my hiking apps for devices without onboard maps.

  • Hi Jim, thanks for your reply. 

    I am super new to Connect IQ development and what I am trying to do is a super simple DataField that takes the entire screen width. 

    Since I have multiple data to render (which I can easily do with <label> tag defined in layouts.xml) I want to draw lines in between those data just to have the UI more clear (at least to me). 

    I tried dc.drawLine() directly in the onUpdate method but nothing is rendered on my screen. 

    I also tried to define the line in layouts.xml and drawables.xml files but I wasn't success and, most important, I am not sure is the right way. 

    Thanks for helping me!

  • In a simple data field, onUpdate isn't called and layouts aren't used. Only the label and what's returned from compute  You need to use a complex data field.

  • Sorry, my bad. I wrote "super simple" to indicate that my DataField is a dumb Complex DataField

    I am not using SimpleDataField but DataField:

    class MyDataFieldView extends WatchUi.DataField {
  • Remember that the dc for a DF is cleared before onUpdate is called so you must draw the line every time.  What if you add println calls to see what's happening in the sim?

  • Hi Jim,

    I tried different things and I have found the following solutions:

    Setting property (like color or text) for a label (retrieved from layouts.xml with View.findDrawableById("idLabel")) has to be done before calling parent method View.onUpdate(dc);

    Drawing a line with the following code:
    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK);
    dc.setPenWidth(2);
    dc.drawLine(dcWidth / 2, 0, dcWidth / 2, dcHeight);

    Has to be done after calling View.onUpdate(dc); or it is not drawing anything. 

    It works but I don't understand why... :-(

    I also don't understand why in the onUpdate method randomly dc width/height (or even the labels width/height) are 0. 
    This is requiring me put a lot of check to avoid doing something if width/height are 0 (or I have strange "movements" in the layout). 

    Thanks!

  • For colors, you probably want to use getBackgroundColor(), because the backround color can change based on device and user settings, or in some cases time of day (sunrise/set).

    Just a guess but look at when you do things in onLayout and onUpdate.  I always use the witdh/height I see in onLayout for example and consider the case where a DF can be used twice on the same screen

  • I want my DataField to be used once on the same screen.