Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Dan277
Posts: 21
Registered: ‎04-10-2013
My Device: All
Accepted Solution

Get virtual keyboard height

I am writing an application that is designed to work on multiple devices. I have everything sized and laid out according to a percentage of the screen size. On the blackberry 9850 all the items are virtically stretched too much and my program looks a mess. To solve this problem I decided to force the virtual keyboard to display. How can I get the height of the virtual keyboard? I need to get its height so I can make everything fit in the available space. Thank you

 

public NewScreen()
{
   super(NO_VERTICAL_SCROLL|NO_VERTICAL_SCROLLBAR);
   setTitle("Title");
   Manager contentManager = getMainManager();
   Manager screenManager = contentManager.getManager();
   Field titleField = screenManager.getField(0);
   AbsoluteFieldManager manager = new AbsoluteFieldManager();
   if (VirtualKeyboard.isSupported())
      getScreen().getVirtualKeyboard().setVisibility(VirtualKeyboard.SHOW_FORCE); 
   int screenWidth = Display.getWidth();
   int screenHeight = Display.getHeight() - titleField.getPreferredHeight();

 

Please use plain text.
Developer
simon_hain
Posts: 16,215
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Get virtual keyboard height

welcome to the support forums.

first: forcing the virtual keyboard is not a good solution.

when the virtual keyboard is shown your screen should get a smaller size in its layout method, you should be able to work with that.
see
http://supportforums.blackberry.com/t5/Java-Development/How-to-Extend-Manager/ta-p/446749
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get virtual keyboard height

I agree that forcing the virtual keyboard is not a good option.  And it won't work on the Torch with the keyboard out anyway.

 

I am guessing that you have tried to fill your screen by scaling Bitmaps to match the proportions of the screen.  As you have found out, this doesn't work very well on BlackBerry devices because the aspect ratio of the screens is very different.  And it gets worse when you start considering models that tilt.  

 

There are various other techniques that you can use.  The one I use most commonly is to adjust the spacing around the Fields so that the Fields still look OK but can take up a variable amount of vertical space.  The second most common is to have a number of images and choose the one that matches the screen.  I find this technique is the only one that works OK for splash screens that contain aspect ratio specific images, but these images sure bloat the application.  The final technique, which I avoid but is favored by some other people, is to do different builds with different images for different devices. 

 

But the approach you should use depends on the content of your screen and what compromises you are prepared to make.  If you give us specific examples of the difficulties you are having, we can comment on possible solutions for these. 

Please use plain text.
Contributor
Dan277
Posts: 21
Registered: ‎04-10-2013
My Device: All

Re: Get virtual keyboard height

Scaling the bitmaps is not a problem. I only use 1 bitmap and that for my custom objectchoicefield and I've managed to make it resize fine. Everything resizes OK, the only problem is on certain resolutions everything is totally out of proportion I think the best solution would be for just those certain aspect ratios, force the keyboard if possible, and add white space when it's not possible. I am still faced with the same problem. The screen does not resize like it should when the keyboard is displayed and I do not know how to get the height of the keyboard. If I can just figure out its height pragmatically I should be able to get it to display correctly on all resolutions.

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get virtual keyboard height

Can you show us an example?

Please use plain text.
Contributor
Dan277
Posts: 21
Registered: ‎04-10-2013
My Device: All

Re: Get virtual keyboard height

I can but I am not sure it will be very useful because the size of the screen is already calculated in the code I posted in my first post.

 

After calculating the height and width of the screen, this is called:

lblVerbs = new MyLabelField("Verbs:", (int) (screenWidth * 0.2) - xSpacing * 2, fieldHeight);

I pass the width and height to every single field in my program, and they are all sized and positioned based on fractions of the screen size. To size and position them correctly when the virtual keyboard is being displayed, I simply need to alter the screenHeight variable to take into account the new size of the screen. The problem is I'm unable to do that because I can't get the height of the virtual keyboard, and Display.getHeight() returns the same value even when it's called before creating the absolutefieldmanager. Both calculating the screen height and displaying the virtual keyboard are called in the constructor of its class that extends MainScreen, but surely that has to be the first piece of code to be called, and is therefore correct.

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get virtual keyboard height

OK, I don't know how to force the Virtual Keyboard to display before a screen is shown.  I've only ever tried to force it to display when on a field that I know accepts keyboard input.

 

I will have one more go at trying to persuade you it is not a good idea, then give up.  And I will think about how you might be able to get it displayed.  Might it might be convoluted.  But I think sublayout might be invoked when the virtual keyboard is displayed and at that time, Display.getHeight() reflects the height visible above the keyboard.  So you might have to override sublayout and adjust all your Field sizes in there. Will that work for you? 

 

Anyway I think attempting to fix the height in this way will fail in two cases

a) Torch which I suspect will never display a virtual keyboard with the slider out.  So your screen will always be tall and skinny which I think means your display will not look as you intended. 

b) Any tiltable screen, in landscape mode, will I suspect give you a very distorted screen especially with the keyboard displayed. 

 

Are you prepared to have these cases not work correctly for you?

Please use plain text.
Contributor
Dan277
Posts: 21
Registered: ‎04-10-2013
My Device: All

Re: Get virtual keyboard height

I am not prepared to have these cases not display correctly for me, but I am prepared to code for them. I already have have my program allow the keyboard to hide when the screen is tilted horizontally. I already have overwritten sublayout so that I can resize and reposition all of my fields when the screen is tilted. Unfortunetly, as soon as my program resizes one of the labels, I get WARNING: Cannot layout field, insufficient height or width, followed by an IllegalStateException. I won't post code relating to that yet because I should probably try everything I can think of to get it to work first. Thank you for your help.

Please use plain text.
Developer
peter_strange
Posts: 19,609
Registered: ‎07-14-2008
My Device: Not Specified

Re: Get virtual keyboard height

"followed by an IllegalStateException."

 

I'm guessing this is because you are doing something that should result in a layout call, within layout.  Print out the detail for the IllegalStateException to find out (Exception.toString() will do)

 

If, during sublayout, I find that I need to do something that would effect layout, for example remove a Field, then the only way around this (that I am aware off), is to complete the layout processing, and do what you really want to do in a separately invoked process.  Basically let sublayout do whatever is easiest, but use an invokeLater Runnable to do the processing you want to do that involves layout.  Hope that makes sense. 

 

This does mean the screen will look silly for a second as the original sublayout laid out Fields are displayed, but then the invokeLater Runnable kicks in and you change your Fields, which means layout gets invoked again, and it works correctly the second time.  Then the screen looks OK. 

 

You need to make sure that this is not infinitely recursive of course. 

Please use plain text.
Contributor
Dan277
Posts: 21
Registered: ‎04-10-2013
My Device: All

Re: Get virtual keyboard height

I've already found the problem. When setting the size calling setFont(font) caused the crash. I can simply call setFont(font) in my fields' paint functions. I am currently modifiying all of my custom fields so that I can resize all of them without a crash. Thank you for your help.

Please use plain text.