Knowledge Summary of Android Horizontal and Vertical Screen Processing

  • 2021-11-02 02:20:44
  • OfStack

Android mobile phones generally support horizontal and vertical screen rotation, and the system will also provide a setting to control whether rotation is allowed or not. Here is a summary of how to control the rotation direction of the interface in App.

Determinants of Interface Rotation Direction

There are several factors that determine the horizontal/vertical display of an interface:

System settings, 1 can be set to only allow vertical screen or rotatable switching. The direction of the device as sensed by its physical sensors. Different codes in App set horizontal and vertical screens.

One point to note is that these three factors have no fixed priority. Therefore, even if the setting item of the system is fixed as a vertical screen, the code in App can also set the interface as a horizontal screen, of course, the scope of influence is limited to the internal interface of App.

Method of Setting Horizontal and Vertical Screen in Code

We have two ways to set up horizontal and vertical screens:

In the AndroidManifest. xml file, the < activity > Tag sets the android: screenOrientation attribute, and the specific values and effects are shown below.
Call Activity. setRequestedOrientation in Java (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE), the values and effects of which are described below.

If it is set in xml, the orientation of the interface has been determined when it is initialized. If it is set in Java, it can be dynamically set according to different situations.

Values and effects passed in when setting horizontal and vertical screens

The following is listed in AndroidManifest. xml < activity > Possible values for the android: screenOrientation attribute of the tag. If set in Java mode, the parameter corresponds to 11. (Refer to official documents)

unspecified: Default, no specifying, the display direction is determined by the system. This will be determined by the last activity, or by the direction set by the user when activity is at the bottom of the stack. landscape: Keep the horizontal screen. portrait: Maintain vertical screen. user: The orientation value currently set by the user. behide: Maintain direction 1 with the previous Activity. sensor: Depends entirely on the direction of the physical sensor. Note that using this value ignores the user's rotary switch status in system settings. (Note that 1 machine does not support 180 degrees rotation of vertical screen even if it uses this value) nosensor: Ignore the direction of the physical sensor. This will cause the user to rotate the phone without switching the horizontal and vertical screens. sensorLandscape: Maintain the horizontal screen, but the direction of the horizontal screen can be determined according to the physical sensor. sensorPortrait: Maintain the vertical screen, but the direction of the vertical screen can be determined according to the physical sensor. reverseLandscape: Maintain the horizontal screen, but in the opposite direction to when using landscape. reversePortrait: Maintain the vertical screen, but in the opposite direction to when using portrait. fullSensor: Same as sensor, except that this property allows you to rotate in all four directions. userLandscape: The horizontal screen is maintained, but the direction of the horizontal screen can be determined according to the physical sensor when the user allows rotation. (Note the comparison with sensorLandscape) userPortrait: Maintain the vertical screen, but the direction of the vertical screen can be determined according to the physical sensor when the user allows rotation. (Note the comparison with sensorPortrait) fullUser: Same as user, except that if the user allows rotation, this property allows rotation in all four directions. locked: The screen orientation will be locked in the current direction and can no longer be rotated.

There are many values of this attribute, but there are only a few commonly used values: unspecified (rotation allowed), landscape (fixed as horizontal screen), portrait (fixed as vertical screen). Moreover, we only need to specify a fixed value in AndroidManifest. xml. Unless you have your own set of logic for switching between horizontal and vertical screens, you need to make different settings under different logic.

Activity recreation caused by horizontal and vertical screen switching

By default, switching between horizontal and vertical screens causes the current Activity to be destroyed and then recreated. Therefore, we can make some differences according to the current horizontal and vertical screen status when Activity is recreated. The most common is to use different layout xml according to the horizontal and vertical screens.

Establish layout-land and layout-port directories under res directory, put one layout xml file with the same name in different directories respectively, and then use this layout file normally in the code, and the system will automatically use the corresponding layout file when switching between horizontal and vertical screens and recreating Activity. Such as:


@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_layout);
}

If you don't want to distinguish the horizontal and vertical screen layout by creating multiple layout files, you can also use Java code to make some differentiation. You can get the information of the current horizontal and vertical screen by using the following judgment.


@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_layout);
  //  Get horizontal and vertical screen information 
  int orientation = getResources().getConfiguration().orientation;
  if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
    //  Do it for the horizontal screen 1 Some treatment 
  } else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
    //  Do it for vertical screen 1 Some treatment 
  }
}

One point to note is that the lifecycle of Activity has been called again, and some information says:

When the android: configChanges property of Activity is not set, the lifecycle is re-executed once when cutting to the horizontal screen and twice when cutting to the vertical screen.

If the android: configChanges= "orientation" attribute is set, it will only be executed once.

However, personal experiments have not verified this statement. To be on the safe side, you can add the android: configChanges = "orientation" attribute to ensure that the life cycle is called only once.

Prevent Activity from restarting when switching between horizontal and vertical screens

Although we can use the above method to make different layouts for horizontal and vertical screens, the re-creation of Activity caused by switching between horizontal and vertical screens will also cause the loss of users' current status and data. For example, text entered to 1.5 in EditText will be lost after Activity is recreated. Therefore, if there is a way to rotate the screen without recreating Activity, it should be a better choice.

We can use AndroidManifest. xml for the corresponding < activity > Set the android: configChanges property so that it does not cause the recreation of Activity when the horizontal and vertical screens are switched.

SDK prior to Android 3.2 (API Level 13) can be configured as follows


android:configChanges="orientation|keyboardHidden" 

SDK after Android 3.2 needs to be set to


android:configChanges="keyboardHidden|orientation|screenSize" 

In this way, Activity will not be recreated when the horizontal and vertical screens are switched.

Callback when switching horizontal and vertical screens

1 Once we set the android: configChanges attribute according to the above method, Activity will not be recreated when the horizontal and vertical screens are switched, so how do we perceive it in code when the screen rotates? The onConfigurationChanged method of Override, Activity and View can be used.


@Override 
public void onConfigurationChanged(Configuration newConfig) { 
  super.onConfigurationChanged(newConfig);
  if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
    //  Do it for the horizontal screen 1 Some treatment 
  } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
    //  Do it for vertical screen 1 Some treatment 
  } 
}

Determine the current screen direction

With the above callback of horizontal and vertical screen switching, we can sense the state change of the screen in time. However, because the vertical screen and the horizontal screen have two different directions, the Android system provides us with a method to obtain the accurate rotation direction of the current screen.


Display display = activity.getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
//
// rotation  Indicates the rotation angle of the current screen. Possible values are :
// Surface.ROTATION_0 , Surface.ROTATION_90 , 
// Surface.ROTATION_180 , Surface.ROTATION_270 . 
//

In this way, we can judge the direction of the current screen.

For example, the following method gets the rotation direction of the current screen, and then locks the rotation direction so that the user can no longer rotate.


public static void lockScreenOrientation(Activity activity) {
  Display display = activity.getWindowManager().getDefaultDisplay();
  switch (display.getRotation()) {
    case Surface.ROTATION_90:
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
      break;
    case Surface.ROTATION_180:
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
      break;
    case Surface.ROTATION_270:
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
      break;
    default:
      activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  }
}

Summarize

android: The screenOrientation attribute can control the direction of Activity. The commonly used values are unspecified (default, rotatable), landscape (keeping horizontal screen) and portrait (keeping vertical screen). If you want Activity to be destroyed and recreated when triggering the horizontal and vertical screen switch, you can set android of Activity: configChanges= "orientation". If you do not want Activity to be destroyed and recreated when triggering the horizontal and vertical screen switch, you can set android of Activity: configChanges= "keyboardHiddenorientationscreenSize". After that, when the horizontal and vertical screens are switched, you can handle the things after switching in the Activity. onConfigurationChanged or View. onConfigurationChanged methods. You can use getResources (). getConfiguration (). orientation at any time to get the horizontal and vertical screen status of the screen. You can use activity. getWindowManager (). getDefaultDisplay (). getRotation () to get the angle of rotation of the screen at any time.

Related articles: