How get height of title screen on blackberry screen? - blackberry

Why getHeight after setTitle equal null?
How do I get the actual height value?
public class ActivityScreen extends MainScreen {
/**
* Creates new instance Activity screen
*/
public ActivityScreen() {
super();
TitleFieldManager titleField = new TitleFieldManager(Display.getWidth());
super.setTitle(titleField);
if (titleField.getHeight() == 0) {
// Why titleField.getHeight() == 0 ?
}
}
}

Because you are calling titleField.getHeight() in the screen's constructor. At this point the UI framework has not passed the layouting/measuring process for the screen yet. If for instance, you will call the same titleField.getHeight() after at least one screen's paint(Graphics graphics) call has been executed you'll be able to get a non-zero value. All layouting/measuring is guaranteedly passed BEFORE the screen content can be drawn.
Check the Manager.sublayout(int width, int height) API. MainScreen is also a Manager. So at some point AFTER the screen has been constructed, but BEFORE it is painted, the UI framework calls its sublayout(int width, int height) where all layouting/measuring happens (all child fields get their sizes).

I'm not 100% sure of this, but I think the height of the element is calculated when the manager that contains it is laid out.
Try pushing the screen that contains that title and then calling titleField.getHeight()

Related

Horizontally centering a popup window in Vaadin

I have added a popup window to my main UI as follows:
Window component = new Window();
UI.getCurrent().addWindow(component);
Now, I want my popup to be centered horizontally and e.g. 40 pixels from the top of the screen. As far as I can see Vaadin has 4 methods for positioning my window.
component.center()
component.setPosition(x, y)
component.setPositionX(x)
component.setPositionY(y)
None of these are really what I want. I was hoping at first that setPositionY might help me. This does allow me to get the right distance from the top, but the x-position is now set to 0, where I wanted it to be centered.
The setPosition might have helped if I was able to calculate what the x-position should be, but this would require me to know the width of the component in pixels, but component.getWidth just tells me 100%.
Next I tried to use CSS styling on the component, writing and explicit css rule and adding it to the component with addStyleName. It seems though that Vaadin overrides whatever I wrote in my css with its own defaults...
Any ideas how to get my Window component positioned correctly?
I used the methods getBrowserWindowWidth() and getBrowserWindowHeight() from the com.vaadin.server.Page class for this.
I centered my "log" window horizontally in the lower part of the browser window with
myWindow.setHeight("30%");
myWindow.setWidth("96%");
myWindow.setPosition(
(int) (Page.getCurrent().getBrowserWindowWidth() * 0.02),
(int) (Page.getCurrent().getBrowserWindowHeight() * 0.65)
);
Solution 1: Use SizeReporter
Indeed, setPositionY() will reset the window's centered property to false. As the width of your pop-up and that of your browser window are not know before they appear on the screen, the only way I know to get those values is to use the SizeReporter add-on. Its use is quite straightforward:
public class MyUI extends UI {
private Window popUp;
private SizeReporter popUpSizeReporter;
private SizeReporter windowSizeReporter;
#Override
protected void init(VaadinRequest request) {
Button button = new Button("Content button");
VerticalLayout layout = new VerticalLayout(button);
layout.setMargin(true);
popUp = new Window("Pop-up", layout);
popUp.setPositionY(40);
addWindow(popUp);
popUpSizeReporter = new SizeReporter(popUp);
popUpSizeReporter.addResizeListenerOnce(this::centerPopUp);
windowSizeReporter = new SizeReporter(this);
windowSizeReporter.addResizeListenerOnce(this::centerPopUp);
}
private void centerPopUp(ComponentResizeEvent event) {
int popUpWidth = popUpSizeReporter.getWidth();
int windowWidth = windowSizeReporter.getWidth();
if (popUpWidth == -1 || windowWidth == -1) {
return;
}
popUp.setPositionX((windowWidth - popUpWidth) / 2);
}
}
This piece of code will be okay as long as you don't resize the pop-up. If you do, it will not be automatically recentered. If you replace addResizeListenerOnce() by addResizeListener() then it will automatically recenter the pop-up but you'll get some "UI glitches" as the add-on sends resize events almost continually while you're resizing your pop-up...
You could try to do it using CSS, but I personally avoid CSS as much as I can with Vaadin :).
You'll need to recompile the widgetset after you've added the add-on as a dependency.
Solution 2: Use com.vaadin.ui.JavaScript
I won't vouch for the portability of this solution but I guess it will work on most modern browsers.
public class MyUI extends UI {
private Window popUp;
#Override
protected void init(VaadinRequest request) {
Button button = new Button("Content button");
VerticalLayout layout = new VerticalLayout(button);
layout.setMargin(true);
popUp = new Window("Pop-up", layout);
popUp.setPositionY(40);
popUp.addStyleName("window-center");
addWindow(popUp);
// Add a JS function that can be called from the client.
JavaScript.getCurrent().addFunction("centerWindow", args -> {
popUp.setPositionX((int) ((args.getNumber(1) - args.getNumber(0)) / 2));
});
// Execute the function now. In real code you might want to execute the function just after the window is displayed, probably in your enter() method.
JavaScript.getCurrent().execute("centerWindow(document.getElementsByClassName('window-center')[0].offsetWidth, window.innerWidth)");
}
}

Unable to change width of TextField in Blackberry

I am a newbie to Blackberry, I was just trying out some sample apps in Blackberry. I tried to create a login page. In that, when I tried to change the width of the text field, the text field became invisible.
The below is part of the code to create the TextField.
super(Field.FIELD_VCENTER);
......
t_username = new TextField()
{
public void layout(int width, int height)
{
super.layout(500, 30);
setExtent(500, 30);
}
};
t_username.setMaxSize(10);
t_username.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1),Border.STYLE_SOLID));
I tried to create a border to check where it is coming or not, but couldn't find it.
PFB, the snapshot:
Overriding the TextField#layout() method
public void layout(int width, int height)
{
super.layout(500, 30);
setExtent(500, 30);
}
is one way that you can set a text field's width. So, I think there's something else going wrong here.
1) Possibly, when you changed your code, your mistakenly removed the call to
add(t_username);
you don't show us where you actually add that field, so if you're not calling add(t_username) somewhere else, it's not going to be visible. Fields must be added to a Manager or Screen to be visible.
2) Perhaps some other code you've written (but not shown) is attempting to do something with a Graphics object. For example, if you're overriding a paint(Graphics) method in another field, you may be changing a color (e.g. graphics.setColor(Color.WHITE)) and not remembering to reset the original color. Possibly, your text field is there, but it's the same color as its background. If this was happening, though, you could still see the text field cursor when you give that field focus. I simply can't tell from your screenshot.
A Better Way
Normally (but not always), it should be the responsibility of the Manager/Screen that contains the field to determine its size. I think it's poor encapsulation to have most fields set their own width (although there are exceptions to this). I would recommend using setMargin() and the USE_ALL_WIDTH flag to set a reasonable width for this text field:
public class TextFieldScreen extends MainScreen {
private TextField t_username;
public TextFieldScreen() {
super(Field.FIELD_VCENTER);
HorizontalFieldManager row = new HorizontalFieldManager();
LabelField label = new LabelField("Username");
label.setMargin(new XYEdges(2, 0, 2, 10));
row.add(label);
t_username = new TextField(TextField.USE_ALL_WIDTH);
t_username.setMaxSize(10);
t_username.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,1,1,1), Border.STYLE_SOLID));
t_username.setMargin(new XYEdges(2, 10, 2, 10));
row.add(t_username);
add(row);
}
}
One benefit of this solution, compared to hard-coding a width of 500, is that if your app supports portrait/landscape rotation, the code above will correctly adjust the field width as the screen width changes. If you hard-code the field width, the field will be too wide when the device rotates to portrait.

Blackberry VerticalfieldManager virtual scroll view size too large

Developing on the blackberry (OS 7.0) and I have an extended Vertical Field manager created as such:
_myVFM = new MyViewManager(Manager.USE_ALL_WIDTH | Manager.USE_ALL_HEIGHT | Manager.VERTICAL_SCROLL){};
However, when I scroll the view, the virtual scroll view size appears way too big.
i.e, I can scroll quite alot further down than is needed and I cant work out why?
Any body any quick ideas? I do have a background image in there that is created as such:
public void paint(Graphics graphics)
{
Bitmap backgroundBitmap = Bitmap.getBitmapResource("bg.png");
Bitmap newBackground = new Bitmap(Display.getWidth(), Display.getHeight());
backgroundBitmap.scaleInto(newBackground, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
graphics.clear();
graphics.drawBitmap(0, 0, Display.getWidth(),
Display.getHeight(), newBackground, 0, 0);
super.paint(graphics);
}
Please and thanks,
Burrows
You can redefine the method to define the height of the manager
protected void setExtent(int width, int height) {
super.setExtent(width, myHeight);
}
To not repeat the background image is necessary to redefine the following method, returning false
protected boolean isScrollCopyable() {
return false;
}
Another comment is that it is a bad practice to obtain an image from the paint method. What you are doing is every time you call the paint method is going to get this image.
It's best to get the image once and then use it.
public MyScreen() {
super(NO_VERTICAL_SCROLL);
I managed to fix the issue in my extension of MainScreen using 'NO_VERTICAL_SCROLL' style parameter
Combined with Rupak's suggestion of setting the Background in the constructor of my Vertical Field Manager rather than overriding paint (
https://gist.github.com/3248319)
everything seems Good now - thanks all for your help.
Burrows
No need to USE_ALL_HEIGHT in your constructor.
// instead use this
_myVFM = new MyViewManager(Manager.USE_ALL_WIDTH | Manager.VERTICAL_SCROLL){};

How to Increase the row height of the selected item of listfield

I am using a ListField for my app to show the data in a list. Now my requirement is that i want to increase the row height of the selected item of the list.
For this, i created a GridFieldManager and populated my list using this manager. I then override the onFocus() method of this manager. But, this method is never invoked. As a result i am unable to increase the height of the selected row item.
Please help.
ListField rows are all designed to be uniformly sized, so unfortunately you won't be able to directly change the size of one row. It's possible you can emulate it by setting it so that the row above and below draw a little bit of the focus color at the bottom and top respectively. Alternatively, you could have one field that just stays centered above whatever row is focused. Then redraw this "floating Field" with the information from the selected row.
I got it working. I faked the ListField implementation. I removed ListField and replaced it with VerticalFieldManager. I adjusted its size during onfocus() and onUnfocus() and used sublayout() to change the height of this manager. Its working exactly the way i want.
you can increase the row height/font text/...
as ultimately, all of the api's call canvas's paint methods
how to do:
//try playing with the font_type,font height
Font myFont = Font.getDefault().derive(Font.PLAIN, 14, Ui.UNITS_px);
private class ListCallback implements ListFieldCallback {
public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
g.setFont(myFont);
String text = (String)listElements.elementAt(index);
g.drawText(text, 0, y, 0, w);
//you can increase the height of a particular row in the height parameter.
// if you want it to be specific, put an 'if conditon'
EX: if(index=1)
{
w+=10;
}
}
}

Blackberry setting the position of a RichtextField in FullScreen

I am writing an application in BlackBerry, where I want to do some custom painting at the top portion of the screen in the paint method of FullScreen and at the same time, I want a RichtextField positioned at the lower portion of the screen. I tried using setPosition methods in the Field class, but to no avail. So how do I set the position of the RichtextField that is added to the FullScreen class?
You can use a SpacerField for that purpose:
class SpacerField extends Field {
int localWidth, localHeight;
SpacerField(int width, int height) {
super(Field.NON_FOCUSABLE);
localWidth = width;
localHeight = height;
}
protected void layout(int width, int height) {
// TODO Auto-generated method stub
setExtent(localWidth, localHeight);
}
protected void paint(Graphics graphics) {
}
public int getPreferredWidth() {
return localWidth;
}
public int getPreferredHeight() {
return localHeight;
}
}
and add it to your Screen before your RichTextField. Be sure to give a suitable width (Display.getWidth() ?) and height when constructing the SpacerField.
Note: I had found the code at this forum discussion a few months ago when I needed to do something similar.
The best way to position objects is to extend a Manager and use it to position and size the objects the way you want. Check the documentation for net.rim.device.api.ui.Manager and net.rim.device.api.ui.Field for information on how manager control their children.

Resources