Android Force setting horizontal screen to determine whether it is a horizontal screen or vertical screen

Source: Internet
Author: User

Determine if activity is a horizontal or vertical screen

Method 1://Based on device configuration information

Configuration cf= this.getresources (). GetConfiguration (); Get configuration information for a setting

int ori = cf.orientation; Get screen orientation

if (ori = = cf. Orientation_landscape) {

Horizontal screen

}else if (ori = = cf. orientation_portrait) {

Vertical screen

}

Method 2: Through the device resolution also judge

Displaymetrics dm = new Displaymetrics ();

Mlauncher.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);

Mwidth = Dm.widthpixels;

Mheight = Dm.heightpixels;

if (Mheight > Mwidth) {//layout port

Vertical screen .....

}else{//layout Land

Horizontal screen .....

}

There are two ways to set up a program horizontal or vertical screen 1: By configuring Internship 2: Code implementation

Method 2://Overriding the activity Onresume method

@Override protected void Onresume () {

/** * Set to horizontal screen */

if (Getrequestedorientation ()!=activityinfo.screen_orientation_landscape) {setrequestedorientation ( Activityinfo.screen_orientation_landscape);

Activityinfo.screen_orientation_landscape: Horizontal

Screen_orientation_portrait: Vertical

}

Super.onresume ();

}

Method 2: Configure the Androidmanifest.xml implementation

Add the android:screenorientation= "landscape" information to the activity to have two values for this property portrait//vertical landscape//Horizontal

<activity android:name= "mainactivity" android:label= "@string/app_name" android:screenorientation= "Landscape" >

</activity>

Come on, try it. But careful friends will find that when my screen changes in the direction of why the activity of the OnCreate how to reload it

The reason: Because the rotation of the screen will create the activity element of course to turn off to create activity reload, obviously this is sometimes not the effect we want, then we look at if we solve the problem

We add a red attribute to the Activity node in the Androidmanifest.xml file

<activity android:name= "mainactivity" android:label= "@string/app_name" android:screenorientation= "Landscape" android:configchanges= "Orientation|keyboardhidden">

</activity>

1, do not set the activity of the android:configchanges, the screen will recall the various life cycle, cut across the screen will be executed once, cut the vertical screen will be executed twice

2, set the activity android:configchanges= "orientation", the screen will recall the various life cycle, cut horizontal, vertical screen will only be executed once

3, set the activity android:configchanges= "Orientation|keyboardhidden", the screen will not recall the various life cycle, will only execute onconfigurationchanged method

It also overloads the Onconfigurationchanged (Configuration newconfig) method in the activity's Java file so that it does not overload during layout switching or window switching. The code is as follows:

@Override
public void onconfigurationchanged (Configuration newconfig)
{
Super.onconfigurationchanged (Newconfig);
if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape)
{
Land
}
else if (this.getresources (). GetConfiguration (). Orientation = = configuration.orientation_portrait)
{
Port
}
}

//------------------------------------------------------

The question of how to toggle the activity in Android can be configured with activity in the Androidmanifest.xml file:
android: screenorientation =["Unspecified" | "User" | "Behind" |

"Sensor" | "Nonsensor"]
Screenorientation is used to specify the direction in which activity is displayed on the device, each of which represents the following meanings:
"unspecified" The default value is determined by the system to determine the direction of the display. The decision strategy is related to the device, so different devices will have different display directions.
"landscape " horizontal screen display (width to length)
"portrait " vertical display (high width to long)
"user " the user's current preferred orientation
"behind " is in the same direction as the activity below the activity (in the activity stack)
"sensor " The has a physical sensor to decide. If the user rotates the device, the screen will switch between the screens.
"nosensor" The physical sensor is ignored so that it does not change as the user rotates the device (except for the " unspecified " setting).

Android Force setting horizontal screen to determine whether it is a horizontal screen or vertical screen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.