Android Dynamically changes screen orientation

Source: Internet
Author: User

Landscape and portrait

Example description

How do you program the display direction of activity? In Android, to change the orientation of the screen display through the program, you must overwrite the Setrequestedorientation () method, and to get the current screen orientation, You need to access the Getrequestedorientation () method.

This example, for a brief demonstration of the change practice, designed a button that, when clicked, determines the current screen orientation, such as a vertical row (PORTRAIT), and changes it to horizontal (LANDSCAPE), or horizontal (LANDSCAPE), to Vertical (PORTRAIT) , the example is very simple. Figure 5-25 shows the result of the run.

This program overrides the Setrequestedorientation () method, which is intended to capture the events that are triggered while setting the screen orientation, and, when changed, displays the direction to be changed with a toast.

Sample Program

Src/irdc.ex05_22/ex05_22.java

The beginning of the program (OnCreate) to determine whether the value of getrequestedorientation () is-1, if this value is-1, indicating that there is no value set android:screenorientation in the Activity property, This means that even clicking a button does not determine the orientation of the screen and does not change the direction of the event.

In the overridden Setrequestedorientation () event, the direction constant (Requestedorientation) to be converted is passed in as an integer type with Screen_orientation_ Portrait and Screen_orientation_lan-dscape two specified constants.

/* Import program slightly */

Import Android.content.pm.ActivityInfo;

Import Android.view.Display;

public class Ex05_22 extends Activity

{

Private TextView mTextView01;

Private Button MButton01;

/** called when the activity is first created. */

@Override

public void OnCreate (Bundle savedinstancestate)

{

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

MButton01 = (Button) Findviewbyid (R.id.mybutton1);

mTextView01 = (TextView) Findviewbyid (R.ID.MYTEXTVIEW1);

if (Getrequestedorientation () ==-1)

{

Mtextview01.settext (Getresources (). GetText

(r.string.str_err_1001));

}

/* When you click the button to rotate the screen */

Mbutton01.setonclicklistener (New Button.onclicklistener ()

{

@Override

public void OnClick (View arg0)

{

/* Method One: Rewrite Getrequestedorientation */

/* If you cannot get the screenorientation attribute */

if (Getrequestedorientation () ==-1)

{

/* Prompt cannot perform screen rotation function, because cannot discriminate orientation */

Mtextview01.settext (Getresources (). GetText

(r.string.str_err_1001));

}

Else

{

if (getrequestedorientation () = =

Activityinfo.screen_orientation_landscape)

{

/* If current is horizontal, change to vertical rendering */

Setrequestedorientation

(activityinfo.screen_orientation_portrait);

}

else if (getrequestedorientation () = =

activityinfo.screen_orientation_portrait)

{

/* If currently vertical, change to horizontal rendering */

Setrequestedorientation

(Activityinfo.screen_orientation_landscape);

}

}

}

});

}

@Override

public void setrequestedorientation (int requestedorientation)

{

TODO auto-generated Method Stub

/* Determine the direction you want to change, with Toast prompt */

Switch (requestedorientation)

{

/* Change to Landscape */

Case (Activityinfo.screen_orientation_landscape):

Mmaketexttoast

(

Getresources (). GetText (R.STRING.STR_MSG1). ToString (),

False

);

Break

/* Change to Portrait */

Case (activityinfo.screen_orientation_portrait):

Mmaketexttoast

(

Getresources (). GetText (R.STRING.STR_MSG2). ToString (),

False

);

Break

}

Super.setrequestedorientation (requestedorientation);

}

@Override

public int getrequestedorientation ()

{

TODO auto-generated Method Stub

/* This overrides the Getrequestedorientation method to get the current screen orientation */

return Super.getrequestedorientation ();

}

public void Mmaketexttoast (String str, Boolean Islong)

{

if (islong==true)

{

Toast.maketext (Ex05_22.this, str, toast.length_long). Show ();

}

Else

{

Toast.maketext (Ex05_22.this, str, toast.length_short). Show ();

}

}

}

Androidmanifest.xml

Note that the Android:screenorientation property of the activity needs to be set in Androidmanifest.xml, otherwise the program will not be able to pass the Getrequestedorientation () method, To determine the current direction of activity.

<manifest< p= "" >

Xmlns:android= "Http://schemas.android.com/apk/res/android"

Package= "Irdc.ex05_22"

Android:versioncode= "1"

Android:versionname= "1.0.0" >

<application< p= "" >

android:icon= "@drawable/icon"

Android:label= "@string/app_name" >

<activity< p= "" >

Android:name= ". Ex05_22 "

Android:label= "@string/app_name"

android:screenorientation= "Portrait" >

Extended Learning

In the above program, to call the Getrequestedorientation () method to determine when the button is clicked, the display direction of the screen although the program can also be judged, but the following methods can be used in the aspect ratio of the phone.

/* Method Two: Judging the screen width/height ratio */

Final Display Defaultdisplay =

GetWindow (). Getwindowmanager (). Getdefaultdisplay ();

int h= defaultdisplay.getheight ();

int w = defaultdisplay.getwidth ();

/* This resolution is the button click the current resolution */

Mtextview01.settext

(integer.tostring (h) + "X" +integer.tostring (w));

/if (W > H)

{

/* Landscape */

/* Rewrite the Setrequestedorientation () method in activity */

Setrequestedorientation

(activityinfo.screen_orientation_portrait);

}

Else

{

/* Portrait */

Setrequestedorientation

(Activityinfo.screen_orientation_landscape);

}

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.