How to replicate SimpleDataField view format using DataField?

I am using the DataField class as there is functionality I need which SimpleDataField does not allow. However, the DataField I have now is not formatted the same as the SimpleDataField does by default (the same as the standard Garmin fields). Is there a way to replicate this?

I created this using the new DataField project template, so the onLayout function is as follows:
// Set your layout here. Anytime the size of obscurity of
// the draw context is changed this will be called.
function onLayout(dc) {
var obscurityFlags = DataField.getObscurityFlags();

// Top left quadrant so we'll use the top left layout
if (obscurityFlags == (OBSCURE_TOP | OBSCURE_LEFT)) {
View.setLayout(Rez.Layouts.TopLeftLayout(dc));

// Top right quadrant so we'll use the top right layout
} else if (obscurityFlags == (OBSCURE_TOP | OBSCURE_RIGHT)) {
View.setLayout(Rez.Layouts.TopRightLayout(dc));

// Bottom left quadrant so we'll use the bottom left layout
} else if (obscurityFlags == (OBSCURE_BOTTOM | OBSCURE_LEFT)) {
View.setLayout(Rez.Layouts.BottomLeftLayout(dc));

// Bottom right quadrant so we'll use the bottom right layout
} else if (obscurityFlags == (OBSCURE_BOTTOM | OBSCURE_RIGHT)) {
View.setLayout(Rez.Layouts.BottomRightLayout(dc));

// Use the generic, centered layout
} else {
View.setLayout(Rez.Layouts.MainLayout(dc));
var labelView = View.findDrawableById("label");
labelView.locY = labelView.locY - 16;
var valueView = View.findDrawableById("value");
valueView.locY = valueView.locY + 7;
}

View.findDrawableById("label").setText(Rez.Strings.label);
return true;
}


Cheers
  • I posted in your other thread, but looks like you are now doing a (complex) DF. First of all, what device are you trying to get your DF to look like? And maybe a screen shot of how your code looks in the sim?
  • I created two different threads for the two different projects I'm working on (though they are both facing similar problems). Here, I was trying to modify the label for the data field. This does not appear possible in the SimpleDataField, therefore I am using the DataField instead. This works, but has left me with the formatting issues detailed.

    In the other thread, I was concerned with how to add units to the SimpleDataField (kph etc.). It is mentioned here, but I cannot see how to implement it.

    My goal would be to have my data fields appear as the normal Garmin fields do on any device in any configuration.

    This is how default Garmin data fields appear on the Edge 1000:


    Currently they look like this:

    SimpleDataField (note: no units displayed, but formats correctly otherwise)


    DataField (note: units displayed, and label is updatable during activity, but formatting of label, text and units is off)
  • So you want "RPM" to be
    R
    P
    M
    for example?

    Looks like you need to change the layout and add something like "units1", "units2" and "units3" and put a character in each one (or use direct dc calls)
  • For example, yes. But there's still a lot else that's different between my DataField layout and the ones used by Garmin.

    Regarding updating the label: Like other languages, is there not a way of using SimpleDataField then overriding the onUpdate() method to update my label before calling base.onUpdate()?
  • Like other languages, is there not a way of using SimpleDataField then overriding the onUpdate() method to update my label before calling base.onUpdate()?

    I don't have access to a test environment to verify, but I'm fairly certain that onUpdate() is not called for SimpleDataField. If you want to do your own drawing, you've got to do it all via a DataField-derived class.

    Note: The documentation for View confirms my memory of the situation.