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
Highlighted
New Developer
Posts: 6
Registered: ‎06-08-2011
My Device: Storm 5530
My Carrier: Verizon
Accepted Solution

Get landscape and portrait widths

Hi all,

 

Is it possible to get the landscape width of a screen (or just the Display) while the device is at portrait orientation and without physically rotating/tilting the device? and vice versa?

 

The only way I can think of is to force the screen into, say, landscape orientation (via Ui.getUiEngineInstance().setAcceptableDirections(Display.DIRECTION_LANDSCAPE) ), get Display.getWidth() and then force it back into portrait orientation. I don't know if this will even work or is the best/easiest method.

 

Any help?

Thanks

Developer
Posts: 723
Registered: ‎03-12-2009
My Device: Playbook

Re: Get landscape and portrait widths

I'm wondering, what sort of business requirement would force you to do that?
New Developer
Posts: 6
Registered: ‎06-08-2011
My Device: Storm 5530
My Carrier: Verizon

Re: Get landscape and portrait widths

OK, let me ask it a different way. I have a CustomButton class that extends ButtonField and I use in various other classes in my project. The constructor takes the label of the button and the width of the button. That way I can create a button that's, say, Display.getWidth()/2, or have the width based off of a PopupScreen the button is in. I then override the paint method for my button:

 

protected void paint(Graphics graphics)
	{
		graphics.setColor(curColor);
		graphics.fillRect(0, 0, width(), height + padding);
		setBorder(BorderFactory.createRoundedBorder(borderPad, curColor, Border.STYLE_FILLED));
		graphics.setColor(Color.BLACK);
		graphics.drawText(text, (width() - graphics.getFont().getAdvance(text))/2, (height - graphics.getFont().getHeight())/2);
	}

The width() method is a private method that returns the width of the button. 

 

The problem comes when the phone is tilted (such as on a Storm 9530) the width is fixed. I was looking for a way to dynamically change this when the orientation changes. I thought I could do this by changing the constructor to take the label and then two widths, portrait and landscape:

 

public CustomButton(String text, int landWidth, int portWidth)
	{
		super(Field.FOCUSABLE | ButtonField.CONSUME_CLICK);
		this.text = text;
		this.landWidth = landWidth;
		this.portWidth = portWidth;
		height = Font.getDefault().getHeight();
		curColor = unFocusColor;
        }

 

Then in the width() method decide the orientation of the Display and return the appropriate width:

 

private int width()
	{
		if(Display.getOrientation() == Display.ORIENTATION_PORTRAIT || Display.getOrientation() == Display.ORIENTATION_SQUARE)
			return portWidth;
		if(Display.getOrientation() == Display.ORIENTATION_LANDSCAPE)
			return landWidth;
		return 0;
	}

 

 

Let me know if that doesn't explain my problem clearly. Thanks for the help.

Developer
Posts: 723
Registered: ‎03-12-2009
My Device: Playbook

Re: Get landscape and portrait widths

The surest way to get the current width/height available is to override the appropriate sublayout() method of screen, manager, etc. and update the required values (i.e. update the button's width). i.e. Once the orientation changes, a sublayout is triggered, where you update the buttons extent. Inside the paint function just use the buttons extent for drawing dimensions to get the latest button size based on available width. Base the layout on the values provided by the sublayout call instead of Display.getxxxxx values.
New Developer
Posts: 6
Registered: ‎06-08-2011
My Device: Storm 5530
My Carrier: Verizon

Re: Get landscape and portrait widths

OK I think I got it now.

Thanks for the help!