Skip to content


Making a Custom Screen, Vertically Scrolling and more

It’s been a couple of weeks since my last post - I’ve been busy with a couple of projects (hopefully more on one or two of those eventually), and there are a few posts I have in the works, but they’re all long enough that I don’t have the energy to finish them. Luckily I got inspiration for a new post from this reader comment! It’s fairly simple, but led to me finding a small bug in my GridLayoutManager, and does illustrate some interesting things about BlackBerry layout managers and screens, so read on (and keep those comments coming!):

The question (from Tony Bruce) was:

What happens if you add more fields than you have vertical space? I’m a BB dev newbie but it seams like if you add more fields than you have vertical space then eventually inside sublayout it is going to call layoutChild with a negative height. Is there some extra mojo you have to add if you want the grid to be scrollable so it can be bigger than the screen and the focused field is always visible?

There are two parts to my answer. To get scrolling behavior, just do what I did in my example screen. That is, create a MainScreen, create a GridLayoutManager, and then add the GridLayoutManager to the screen using the add(Field) method. When you add enough items to make the manager layout taller than the available screen height, scroll arrows will appear and the display will scroll to show the focused field. This works because MainScreen’s delegate manager is a VerticalFieldManager with scrolling enabled. What’s a delegate manager? That’s something you need to know if you ever make your own Screen.

Delegate Managers

Although Screen is a subclass of Manager, it actually doesn’t do any of the layout of fields added using its add (and insert) methods directly. Instead, each Screen delegates layout to a manager - the delegate manager. Notice that Screen’s constructor requires a Manager - you specify the Screen’s delegate manager at construction time, and can’t change it later. This means among other things that a MainScreen will always have a scrollable VerticalFieldManager as it’s main area - so if you want other behavior you can’t use MainScreen!

Easy enough - and this explains why we don’t need to do any additional work to the GridFieldManager example to make it scrollable - it’s automatically inside a vertically scrolling manager already! A couple more things about delegate managers, if you’re thinking of making your own screen.

First, the screen gets to set the position and size of its delegate manager. This is done in the layoutDelegate and setPositionDelegate methods. (You don’t have to make your delegate use the entire size of your screen).

Second, don’t worry about laying out any fields that are added to the Screen from within the Screen itself - that’s all done by the delegate. The screen deals with those field in the add (and insert) methods, and then basically forgets about them. This should be obvious but since it derives from Manager, I feel it’s worth saying explicitly - don’t make a Screen the same way you make a Manager.

Part Two: Mea Culpa

Part two will actually be quick - as the commenter pointed out, at some point GridFieldManager might end up setting fields to negative height. As we’ve just seen, in the case where we add it to a MainScreen, this isn’t an issue as we have a huge vertical space to play with. However it is a bug when we’re not in a vertically scrolling manager. So I made a couple of small changes to check that we don’t go above the available height. The interesting change is this, which is a fairly useful convention in a lot of layout methods - it ensures you respect the height (in this case) and width given by your manager:

		setExtent(totalWidth, Math.min(y, height));

Updated in the usual spot.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

Posted in components, ui. Tagged with , , .

7 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Teresa said

    First I wanted to say that GridFieldManager is the highlight of my whole life. Thank you so much for creating it and sharing your code. I couldn’t have done what I did without it. However I was curious how to make the screen/Manager scroll horizontally. I noticed that it is easy to make it vertically scroll but regardless of what properties I set in Manager or screen it will not scroll horizontally. Any insight you could share would be a great help!

  2. Don said

    Hi. Thanks for sharing this! It sounds like it’s definitely what I’ve been looking for. But I do have a couple questions and I hope you don’t mind.

    1) I’d like to create a grid on the screen that does the vertical scroll thing–but below the grid I need to have an option list (drop down style) and a RichTextField. I don’t want these last two fields to move–the scrolling should only happen above them.

    2) I’d like to have each row consist of one “record”. The row would have 4 RichTextFields. I’d like to make it so that double-clicking any place in any row will activate a function to edit that record.

    3) I’d like for each row to alternate in color.

    Would you be able to advise me on these?

    Again–THANKS for sharing this!

  3. danz said

    hi..first of all, your site is help me so much..Thanks a lot..hehehe..
    can i have a question? hope u dont mind..
    1). i going to create a custom popupscreen..but it always shows “the black colored rounded box” behind it, even when i overide paint(Graphics) method..it happen either i extends PopupScreen or Screen..

    can we create a custom PopupScreen without that black box?

    sorry if my question isn’t clear, my english is so bad..lol

    Thanks for sharing all of this..

  4. Partha said

    thanks for your code..
    its very helpfull to me

  5. Nick.Cheng said

    Thanks for your sharing, it’s very helpfull.

    I have one more question:

    Can I redo the FieldManager’s Layout, when some field has been focused. I mean maybe when field focus, it will change it’s size.

    Thank you for your help!

  6. Alexander said

    How can i make a view similar to Blackberry Messenger, MSN or Gtalk ?
    A View with expand and contract list where you show, for instance, Chats, Contacts, Groups. You click on of them and expands to a list of Chats and each element of the list is a Rectangular box with User defined image, name, status (offline, online, busy) as an image status message

  7. Donald said

    Just encountered this great BlackBerry site and spent all this Friday evening on most of the great tutorials. Thx a lot! What I am seeking is to implement a customized list like those app list in App World: with icon to the left and some text to the right. Could you please give some kick start suggestions like what major classes are needed for a screen like that. thx again in advance!

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.