32

I want to change the orientation programmatically while running my Android App, with these lines of code:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

They work so far, but the main problem is that the whole activity is reloaded when the screen orientation changes, and I don't want that. Is it possible? Thanks.

EDIT: OK, after I while I found out what I was missing. I had to include also "screenSize" in the configChanges property, so having

android:configChanges="orientation|screenSize"

solved the whole thing.

luisfer
  • 603
  • 2
  • 8
  • 15
  • I think you should check out the third choice in this answer. I believe that is how PhoneGap handles orientation changes http://stackoverflow.com/questions/5913130/dont-reload-application-when-orientation-changes – MikeIsrael Feb 14 '13 at 11:31
  • You can rotate your activity/fragment view Check this: https://stackoverflow.com/a/23494104/8696548 – ARN Feb 23 '20 at 13:52

5 Answers5

29

See the edit by @luisfer. For targeting Android 3.2 and above, you need BOTH

android:configChanges="orientation|screenSize"

http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

larham1
  • 11,736
  • 5
  • 35
  • 26
4

You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter

Martin
  • 2,146
  • 4
  • 21
  • 32
  • +1, this is the correct answer => what he has asked is not posible, just with this solution he can do what he want, only with API above 13 android:configChanges="orientation|screenSize" –  Sep 12 '13 at 07:24
1

In AndroidManifest file add android:configChanges="orientation" for the activity you want to handle this orientation

In activity use onConfigurationChange overrided method. Do task you want to handle in orientation change.

Raj Shah
  • 322
  • 1
  • 22
  • Thank you, but it still doesn't work. Could it be maybe because I'm using Phonegap/HTML5 in the activity? Thank you. – luisfer Feb 14 '13 at 11:45
  • @RajShah for what need more code? did you read the question? how about the title? –  Sep 12 '13 at 07:25
1

Ansewered here:

Android, how to not destroy the activity when I rotate the device?

Add:

android:configChanges="orientation"

To your androidmanifest.

see:

http://developer.android.com/guide/topics/manifest/activity-element.html#config

Community
  • 1
  • 1
inigoD
  • 1,681
  • 14
  • 26
  • Thank you, but it still doesn't work. Could it be maybe because I'm using Phonegap/HTML5 in the activity? Thank you. – luisfer Feb 14 '13 at 11:44
1

Call this method And Set manifest file

android:configChanges="orientation|screenSize|keyboardHidden"

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

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
kleopatra
  • 51,061
  • 28
  • 99
  • 211
ashah
  • 61
  • 1
  • 2