Screen in landscape mode

Quickly share your Android Code Snippets here...

Screen in landscape mode

Postby nextstep » Thu Nov 06, 2008 3:56 am

Need help in determining if an android(G1) is an landscape mode or not? It would be great if someone could point me to some code snippets or a place in the online manual.

Nextstep
Nextstep
nextstep
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 06, 2008 3:44 am

Top

Postby plusminus » Thu Nov 06, 2008 4:42 am

Probably not the best way, but a working one: (put this to onCreate of your Activity):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         final Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
  2.                 final int h= defaultDisplay.getHeight();
  3.         final int w = defaultDisplay.getWidth();
  4.         this.mIsLandScapeMode = (w > h); // Landscape  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


There is also a probably better method :darrow:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. defaultDisplay.getOrientation()
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


int SCREEN_ORIENTATION_BEHIND Constant corresponding to behind in the screenOrientation attribute. 3
int SCREEN_ORIENTATION_LANDSCAPE Constant corresponding to landscape in the screenOrientation attribute. 0
int SCREEN_ORIENTATION_NOSENSOR Constant corresponding to sensor in the screenOrientation attribute. 5
int SCREEN_ORIENTATION_PORTRAIT Constant corresponding to portrait in the screenOrientation attribute. 1
int SCREEN_ORIENTATION_SENSOR Constant corresponding to sensor in the screenOrientation attribute. 4
int SCREEN_ORIENTATION_UNSPECIFIED Constant corresponding to unspecified in the screenOrientation attribute. -1
int SCREEN_ORIENTATION_USER Constant corresponding to user in the screenOrientation attribute. 2 0x00000002


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby ddtdpro » Tue Jul 07, 2009 9:44 am

plusminus wrote:Probably not the best way, but a working one: (put this to onCreate of your Activity):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         final Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
  2.                 final int h= defaultDisplay.getHeight();
  3.         final int w = defaultDisplay.getWidth();
  4.         this.mIsLandScapeMode = (w > h); // Landscape  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


There is also a probably better method :darrow:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. defaultDisplay.getOrientation()
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


int SCREEN_ORIENTATION_BEHIND Constant corresponding to behind in the screenOrientation attribute. 3
int SCREEN_ORIENTATION_LANDSCAPE Constant corresponding to landscape in the screenOrientation attribute. 0
int SCREEN_ORIENTATION_NOSENSOR Constant corresponding to sensor in the screenOrientation attribute. 5
int SCREEN_ORIENTATION_PORTRAIT Constant corresponding to portrait in the screenOrientation attribute. 1
int SCREEN_ORIENTATION_SENSOR Constant corresponding to sensor in the screenOrientation attribute. 4
int SCREEN_ORIENTATION_UNSPECIFIED Constant corresponding to unspecified in the screenOrientation attribute. -1
int SCREEN_ORIENTATION_USER Constant corresponding to user in the screenOrientation attribute. 2 0x00000002


Regards,
plusminus


I know this thread is long time ago, but i still have a question regrading to it. How can i determine the screen mode during the application runtime? It seems like this method only work during the oncreate function first call, so we know the initial mode of the screen. How do we know that screen changing while we are running the application?
Thank you very much.
User avatar
ddtdpro
Freshman
Freshman
 
Posts: 6
Joined: Sat May 02, 2009 5:39 pm

Postby cadlg » Tue Jul 07, 2009 6:55 pm

By default, your activity will be destroyed and a new instance created (if needed) when a configuration change (such as screen orientation) occurs, so the onCreate method will be called each time the screen orientation changes.

But you can manage this changes, avoiding the destroying of the activity. In this case you would receive a call to your onConfigurationChanged(Configuration) method.

You can find more information in the Activity class documentation page
cadlg
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Feb 20, 2008 12:33 am
Location: Guatemala

Postby ddtdpro » Fri Jul 10, 2009 2:41 pm

Thanks I get it :)
User avatar
ddtdpro
Freshman
Freshman
 
Posts: 6
Joined: Sat May 02, 2009 5:39 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: Exabot [Bot] and 3 guests