Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling display rotation in Android

Tags:

android

I am new the Android. In my application development, I want to handle the display (screen) rotation. Is there any listener is available to handle this event? Or is there any alternate way to handle this situation?

Thanks & Regards,

Bala

like image 375
Balaji.K Avatar asked Jan 30 '11 03:01

Balaji.K


People also ask

How do you handle rotation on Android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

How do I view screen rotation on Android?

If the Android screen rotation not working happens to you , or you're just not a fan of the feature, you can re-enable screen auto-rotate on your phone. Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on.

What happens on screen rotation Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do you handle a dysfunction in the screen reorientation?

Prevent Activity to recreated Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

How do you handle screen rotation in Android?

Android: Efficient Screen Rotation Handling. In Android, when the screen orientation changes (between landscape and portrait) the onCreate method is called which ,if not handled properly, will recreate the entire activity when the result is too simply change the layout and retain all of the same information.

What is screen orientation in Android?

Screen Orientation, also known as screen rotation, is the attribute of activity element in android. When screen orientation change from one state to other, it is also known as configuration change. The initial orientation of the Screen has to be defined in the AndroidManifest.xml file. How to change Screen orientation?

How to turn off screen rotation in deviceconfig?

We need to go to 'Settings' > 'Display' > 'When device is rotated' > Select 'Stay in portrait view'. That will disable the screen rotation for the applications used. Screen rotation specific keys and values do not appear in DeviceConfig.XML anymore when they are generated on the device with PowerTools > EZConfig Generator.

How to determine the current device rotation in Android?

Android has a class called the WindowManager, which can be used to determine the current device rotation via the WindowManager.DefaultDisplay.Rotation property, as shown below:


2 Answers

Let's say you want to handle the orientation change yourself, use configChanges="orientation" in your activity in the manifest.xml

<activity android:name=".MyActivity"
          android:configChanges="orientation"
          android:label="@string/app_name">

Now when one of these configurations change, MyActivity is not restarted. Instead, the Activity receives a call to onConfigurationChanged().

More details here: Handling runtime changes

like image 143
SteD Avatar answered Oct 19 '22 12:10

SteD


When developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), include "screenSize" in addition to "orientation".

android:configChanges="orientation|screenSize"
like image 44
electrobabe Avatar answered Oct 19 '22 13:10

electrobabe