Detect if Orientation changes from portrait to landscape

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Detect if Orientation changes from portrait to landscape

Postby waducu » Fri Oct 30, 2009 3:52 pm

Hi,

I know this topic has already been discussed but it still can't find a method that tells me which tipe of orientation is currently being used.

For example i use android:screenOrientation= "portrait" in my manifest file

When i change the orientation on emulator i would like to rotate just some of the elements in my game like text that is drawn on canvas. For that i must know when the emulator orientation has been changed or the phone has been tilted. is that possible?
waducu
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 30, 2009 3:45 pm

Top

Postby qlimax » Sun Nov 01, 2009 10:12 pm

add android:configChanges node to your Activity's manifest node

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:configChanges="orientation"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


then override this method on your Acrivity

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public void onConfigurationChanged(Configuration newConfig) {
  4.  
  5.  
  6.  
  7. ...
  8.  
  9. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4



http://developer.android.com/reference/ ... ionChanges


bye
:run:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby sandis84 » Mon Nov 02, 2009 2:20 pm

Another neat way to do it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Configuration c = getResources().getConfiguration();
  2.  
  3. if(c.orientation == Configuration.ORIENTATION_PORTRAIT ) {
  4.  
  5.         //lol
  6.  
  7. } else if(c.orientation == Configuration.ORIENTATION_LANDSCAPE ){
  8.  
  9.         //rofl
  10.  
  11. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Now lets code the night away!
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby waducu » Mon Nov 02, 2009 2:33 pm

Thanks guys
waducu
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 30, 2009 3:45 pm

Postby waducu » Mon Nov 02, 2009 4:41 pm

Ok so sansis's method works but it restarts my application and the performance on my game in landscape is at 50% don't know why.. i am building on lunarlander example

So i tried adding android:configChanges="orientation|keyboardHidden" because android:configChanges="orientation" had no effect
so what should i write in the overidden

@Override
public void onConfigurationChanged(Configuration newConfig) {


}
because if i just set some variable or if i do nothing it crashes..
help?
waducu
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 30, 2009 3:45 pm

Postby skul3r » Thu Nov 05, 2009 1:32 pm

waducu wrote:Ok so sansis's method works but it restarts my application and the performance on my game in landscape is at 50% don't know why.. i am building on lunarlander example

So i tried adding android:configChanges="orientation|keyboardHidden" because android:configChanges="orientation" had no effect
so what should i write in the overidden

@Override
public void onConfigurationChanged(Configuration newConfig) {


}
because if i just set some variable or if i do nothing it crashes..
help?


Hi,
Some time ago I solved the same problem using these tags in manisfest: android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden"

Hope it helps
skul3r
Freshman
Freshman
 
Posts: 8
Joined: Fri Jul 03, 2009 2:04 pm

Top

Postby waducu » Thu Nov 05, 2009 1:53 pm

"Hi,
Some time ago I solved the same problem using these tags in manisfest: android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden"

Hope it helps"


Dude good combination,
with these changes and without overidding onConfigChanges() when i press Fn+7 on emulator it rotates the screen without restarting my game :)
But, when that happens i must be alerted some how in order to rotate the texts as well.. how can i do that?
waducu
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 30, 2009 3:45 pm

Postby sandis84 » Thu Nov 05, 2009 4:54 pm

an ugly solution would be a backgroundthread doing my solution would still work. If the "android:configChanges="keyboard|keyboardHidden" makes my solution not work, you could instead measure screen height/width-ratio to detect rotation. I think it would work, but its not pretty. Perhaps someone can come up with a better solution :P
sandis84
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Fri Aug 07, 2009 3:54 pm

Postby saamyandroid » Sun Nov 08, 2009 4:56 pm

i Too have same problem ,


any one here to solve this issue?
saamyandroid
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 09, 2009 8:17 pm
Location: INDIA

Postby waducu » Mon Nov 09, 2009 5:01 pm

"an ugly solution would be a backgroundthread doing my solution would still work. If the "android:configChanges="keyboard|keyboardHidden" makes my solution not work, you could instead measure screen height/width-ratio to detect rotation. I think it would work, but its not pretty. Perhaps someone can come up with a better solution"

I can't do that since the ratio keeps its value:
android:screenOrientation = "portrait"
android:configChanges="keyboard|keyboardHidden"

So when the screen rotates so does the center of coordinates but maybe i didn't understand your ideea :P

Another lead would be to use this code in the activity class together with the changes above

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

global_variable++;

}

So i now know when the screen rotates but i don't know from witch side to witch..
I could ask the user to tell me if he sees the text rotated or not and then each time the global variable increments i know if i have to rotate the text or not..
But i must do this without feedback from the user
Any suggestions?
waducu
Freshman
Freshman
 
Posts: 5
Joined: Fri Oct 30, 2009 3:45 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests