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
Developer
darkberry
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified
Accepted Solution

Unable to detect device rotation from screen sublayout method

I'm developing an app compiled for OS 4.5. I need to find a way of detecting the device rotation and populate again the screen so that the fields are arranged according to the new dimensions.

 

My idea was to save the display width and height in private variables in my screen instance, and from the screen's overriden sublayout method, check whether the new values have changed.

 

What happens is that I keep receiving the old width and height again and again.

 

I've first read this post. In it, @arkadyz correctly points out how the order of calls is a bit odd, and values returned by the Display class are updated after sublayout has completed.  So there's no way to detect it from the sublayout method.

 

The proposed workaround is to check the display dimensions inside an invokeLater. Here's my testing code:

 

public class TestScreen extends MainScreen {

	public TestScreen(){
		super(Manager.NO_VERTICAL_SCROLL);
		populateScreem();
	}
	
	private void populatescreen(){
		deleteAll();
		//fields and containers added here. No need to add any to observe the issue.
	}
	
	public void sublayout(int width, int height){
		super.sublayout(width,height); //Tried calling this in the last line as well with identical result.		
	
		System.out.println(">>>>>>>>> Display.getWidth(before): " + Display.getWidth());
		System.out.println(">>>>>>>>> Display.getOrientation(before): " + Display.getOrientation());
		System.out.println(">>>>>>>>> Width argument value (before): " + width);
		
		UiApplication.getUiApplication().invokeLater(new Runnable(){
			public void run() {
				
				System.out.println(">>>>>>>>> Display.getWidth(later): " + Display.getWidth());
				System.out.println(">>>>>>>>> Display.getOrientation(later): " + Display.getOrientation());
			}
		}, 1000, false); //Playing with the time parameter does not change behavior.
	}
}

 

This yields exactly the same results. after this, i came to this post where @peter_strange

provides a solution based on pushing a dummy screen, which detects the new width and height, and then passing the new values to the previous screen. I tried that too but  It didn't work either.

 

I've also tried using the deprecated Graphics.getscreenWidth instead of Display.getWidth and It didn't work.

 

Now I'm out of luck because I've tried almost everything and I can't provide a solution without resorting to 4.7 or 6.0 APIs.

 

Tested in BB 9550 simulator v5.0.0.535 (Platform 2.13.0.108) and real device BB 9520 v5.0.0.306 (Platform 4.2.0.113)

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable to detect device rotation from screen sublayout method

Thanks for this detailed explanation and sample code.

 

I will look at this later.  Just one thing that you could try for us now. Can you test this on another tiltable device (i.e. not a Storm) like the Torch and see what happens.  Simulator or real device. 

 

Note with a Torch you have to have the slider closed for it to tilt. 

Developer
darkberry
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Unable to detect device rotation from screen sublayout method

Thank you Peter, just tested in a 9810 running OS 6.1 and the same thing happens.
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable to detect device rotation from screen sublayout method

Have a look at the following.  I think code like this can be used to detect orientation changes.

 

public class TestScreen extends MainScreen {

	int currentDisplayWidth = Display.getWidth();
	int currentDisplayOrientation = Display.getOrientation();

	public TestScreen(){
		super(Manager.NO_VERTICAL_SCROLL);
		populatescreen();
	}
	
	private void populatescreen(){
		deleteAll();
		//fields and containers added here. No need to add any to observe the issue.
	}
	
	public void sublayout(int width, int height){
		super.sublayout(width,height); //Tried calling this in the last line as well with identical result.		
	
		System.out.println(">>>>>>>>> Current sublayout width (before): " + currentDisplayWidth);
		System.out.println(">>>>>>>>> Display.getWidth(before): " + Display.getWidth());
		System.out.println(">>>>>>>>> Display.getOrientation(before): " + Display.getOrientation());
		System.out.println(">>>>>>>>> Width argument value (before): " + width);
		if ( Display.getWidth() != currentDisplayWidth ) {
			// process orientation change
			// ....
			// Now reset change detection variables.....
			currentDisplayWidth = Display.getWidth();
			currentDisplayOrientation = Display.getOrientation();
		}
	}
}

 

Developer
darkberry
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Unable to detect device rotation from screen sublayout method

That was the idea, the problem is that Display.getWidth(), Display.getHeight and Display.getOrientation keep returning the same old values. For example:

 

Before rotation: Display.getWidth()=320, Display.getHeight()=240

 

Rotation triggers sublayout. Inside this method, we check again:

  • Expected result: Display.getWidth()=240, Display.getHeight()=320
  • Actual result: Display.getWidth()=320, Display.getHeight()=240

Same story with sublayout arguments.

 

So I tried checking again in invokeLater, but still keps returning old values.

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable to detect device rotation from screen sublayout method

Look at my code, especially the comments in sublayout.  Try it too.  I think you have missed the fact that it has a value that is maintained externally to sublayout. 

Developer
darkberry
Posts: 178
Registered: ‎11-19-2008
My Device: Not Specified

Re: Unable to detect device rotation from screen sublayout method

[ Edited ]

I noticed that. I tried your code anyway, and surprisingly it works. (I say "surprisingly" because adding variables shouldn't change the behavior, but of course that code is supposed to work). I modified the code to remove the two int variables and still it works.

 

I've to further test this, because I had detected a misconfigured setting in the project run configuration. Apparently the module wasn't being deployed to the simulator / devices, so my changes didn't have any effect. And looks like the old cod file being deployed was printing incorrect values somehow.

 

I'm currently triple-checking it, will update the post with my findings.

 

Thanks.

 


UPDATE:

Tested in simulator and real devices, and is working fine. So there was no problem with my initial approach, my eclipse project configuration was wrong.

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: Unable to detect device rotation from screen sublayout method

so a rather mundane solution :smileyhappy:
----------------------------------------------------------
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