Layouts with drawables, bitmaps and text - That's all? I need dynamic subviews

Former Member
Former Member
Still cannot figure out how to add reusable elements to a layout , e.g. a heart rate history or battery status indicator. The current elements in a layout XML are all static, or simple selectables. How would I go about implementing dynamic, structured subviews?
  • Depending on what you needs, there are always direct dc calls (dc.drawText(), etc)
  • Former Member
    Former Member
    That exactly what I wanted to avoid. The whole idea of subviews (or: custom drawables) is their local coordinate system, so you can place them anywhere without changing code.

    I found that I could use the <drawable id="mySubviewId" class="MyModule.MyClassName"> tag, but then there's no way to add x, y, width and height, as the DTD doesn't allow that. So how am I supposed to place these drawables in the view?
  • Are you doing different layouts based on the device/device family?
  • Former Member
    Former Member
    That's the goal. Still, even within a single device family, hard-coding screen coordinates is never a good idea.

    So, how can I implement a custom subview that can be placed anywhere? I'm currently experimenting with custom drawables, but without much success yet. They seem to be ignoring placement in the view with setLocation(), while setSize() seems to work.
  • The picker sample in the SDK demonstrates using different layouts based on the device. Also, in the case of a family with round-240x240.
  • Former Member
    Former Member
    I'm sorry but this doesn't help me at all. I need subviews (or however you call it in Monkey C).

    For example, I have a subview that renders a graph of the HR history. Or another subview that remders a battery icon according to the battery status. Or yet another subview that renders the current velocity of the accel sensor as a moving bar.

    1. Which class do I need to subclass them from?
    2. How do I add them to a view programmatically?
  • Former Member
    Former Member
    It gets better by the minute. Drawables get passed a DC with draw(dc) that is not even translated to the current position of the drawable - Ouch! In the draw() method, each drawable must offset its drawing code with <locX, locY> where it sees fit. Drawing code is not position independent. So what are drawables good for actually, if they can't be translated to any position?

    It's been probably ten years or so since I last used all-caps in a forum, sorry, but this is a HORRIBLE MESS. Even in 1970 this was done better already. I hope this is a bug and will be fixed. Otherwise I seriously consider abandoning this platform altogether.

    I'm now at a last attempt at implementing a class Subview under Drawable that hopefully can deal with this ...
  • I'f I'm understanding what you're asking for, the simple answer is I do it with direct dc calls. Say a function to draw a graph at x,y, with a width= w, and height= h.

    If you do the layout do the dc calls after the View.onUpdate(dc)
  • Have you looked at some of the code for other apps on github, etc? I think you are expected something that's not there, but can be done in other ways.
  • Former Member
    Former Member
    Before each call to draw(dc), the dc needs to be translated to <locX,locY> of the drawable and its clipping region set to <locX,locY, width, height>. This prevents draw(dc) from drawing outside the drawable's bounds and allows all code in draw(dc) to refer to <1,1> as the top-left corner of the drawable.

    This is how every UI under the sun does it. It's impossible to write clean drawing code, without that translation and clipping in dc.

    Since dc already has some clipping methods, I assume the creators did just not get around to implementing the coordinate translation yet.