SoFunction
Updated on 2025-04-04

Summary of Android horizontal and vertical screen switching examples

This example summarizes the relevant techniques for switching horizontal and vertical screens on Android. Share it for your reference, as follows:

1. Switching horizontal and vertical screens is prohibited

Android horizontal and vertical screen switching is common in mobile phone development. In order to avoid unnecessary troubles during horizontal and vertical screen switching during the development process, many software usually prohibit the switching of horizontal and vertical screens, that is, it is achieved by setting the value of the android:screenOrientation property in the activity.

The Android:screenOrientation property has the following parameters:

"unspecified": Default value The system determines the display direction. The determined strategy is related to the device, so different devices will have different display directions.

"landscape": horizontal screen display (longer aspect ratio)

"portrait": vertical screen display (height is longer than width)

"user": The current preferred direction of the user

"behind": Consistent with the direction of the Activity below the Activity (in the Activity stack)

"sensor": There are physical sensors to decide. If the user rotates the device, this screen will switch horizontally and vertically.

"nosensor": Ignore the physical sensor so that it does not change as the user rotates the device (except for the "unspecified" setting).

For example, the following settings

Copy the codeThe code is as follows:
android:screenOrientation="portrait"

Then no matter how the phone changes, the activity with this attribute will be displayed on a vertical screen.

Copy the codeThe code is as follows:
android:screenOrientation="landscape"
Displayed in landscape screen.

The above modification can also be set in Java code by following code

Copy the codeThe code is as follows:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

2. Change layout by switching horizontal and vertical screens

If you want the software to switch between horizontal and vertical screens, the height and width of horizontal and vertical screens may be converted, and different layouts may be required. There are two ways to switch layouts:

1) Create layout-land and layout-port directories in the res directory, and the corresponding layout file names remain unchanged, for example. layout-land is the layout of horizontal screen, and layout-port is the layout of vertical screen. No need to worry about other things. When switching horizontal and vertical screens, the program calls the onCreate method of Activity to load the response layout.

2) If the layout resource is not set as above, you can use Java code to determine whether it is currently horizontal or vertical screen and then load the corresponding xml layout file. Because when the screen becomes horizontal, the system will re-call the onCreate method of the current activity. You can put the following method in your onCreate to check the current direction, and then let your setContentView load different layout xml.

if (().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
   ("info", "landscape"); // Horizontal screen}
else if (().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
  ("info", "portrait"); // Vertical screen}

3. Intercept horizontal and vertical screen transformation through onConfigurationChanged

According to the operation of the second, Activity will call onPause-> onStop-> onDestory-> onCreate-> onStart-> onResume every time it switches horizontally and vertically. This involves the storage and reading of content and data, otherwise the content before the screen is turned away. Many times, such results make the program cumbersome, and for this reason, Android provides the setting of the android:configChanges attribute in manifest, so that the Activity does not continue the above reconstruction process. If the user cares about switching horizontal and vertical screens, you can get the current horizontal and vertical screen parameters in the onConfigurationChanged(Configuration newConfig) function of the Activity or View. As for the order of calling is similar to the order of passing touch time, it does not have the concept of consuming events and will call each onConfigurationChanged function in sequence.

It should be noted that the parameters after horizontal and vertical screen switching can only be obtained in the onConfigurationChanged function. In this function, the dimension position information of the new Layout and control cannot be obtained. If you want to process the dimension and position information, you must call it asynchronously or delayedly through messages. The following is the code that I need to reset the popupWindow position when the project needs horizontal and vertical screen switching:

@Override
protected void onConfigurationChanged(Configuration newConfig) {
   // TODO Auto-generated method stub
   (newConfig);
   //No need to create a Handler in the View, you can directly call the post operation//      new Handler().postDelayed(new Runnable() {
//          @Override
//          public void run() {
//             updatePopup();
//          }
//      }, 500);
   postDelayed(new Runnable() {
       @Override
       public void run() {
          updatePopup();   //
       }
   }, 500);//I have tried to use the post operation directly, and I can also get the correct position in the updatePopup function}

As for how the system flips the window and re-draws it when flipping horizontal and vertical screens, I have not studied the source code in detail, so I don’t know. If you have any understanding, you can tell me.

Of course, if you want to completely prohibit flipping, you can set the property of android:screenOrientation to noseensor, so that the troubles caused by gravity sensing can be ignored. But it doesn't work on the simulator, it's correct on the real machine.

Here is a little knowledge. In the Android emulator, the shortcut key "Ctrl+F11/F12" can realize screen rotation

I hope this article will be helpful to everyone's Android programming design.