LANDSCAPE and PORTRAIT
Example
How can I control the display direction of an Activity through a program? In Android, to change the screen display direction through a program, you must overwrite the setRequestedOrientation () method. To obtain the current screen direction, you need to access the getRequestedOrientation () method.
This example shows how to change the screen. A button is designed. When a button is clicked, the current screen direction is determined, for example, PORTRAIT ), change it to LANDSCAPE. If it is LANDSCAPE, change it to PORTRAIT. The example is very simple. Figure 5-25 shows the running result.
In this program, the setRequestedOrientation () method is rewritten to capture the events triggered when the screen direction is set, and display the direction to be changed with Toast when the screen direction is changed.
Sample program
Src/irdc. ex05_22/EX05_22.java
OnCreate first checks whether the value of getRequestedOrientation () is-1. If this value is-1, No Android: screenOrientation value is set in the Activity attribute, this means that even if you click a button, the screen direction cannot be determined and no change event will be performed.
In the covered setRequestedOrientation () event, the requestedOrientation to be converted is input. The value is of the integer type and has two specified constants: SCREEN_ORIENTATION_PORTRAIT and SCREEN_ORIENTATION_LAN-DSCAPE.
/* Import program omitted */
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 a button to rotate the screen */
MButton01.setOnClickListener (new Button. OnClickListener ()
{
@ Override
Public void onClick (View arg0)
{
/* Method 1: override getRequestedOrientation */
/* If the screenOrientation attribute cannot be obtained */
If (getRequestedOrientation () =-1)
{
/* The system prompts that the image rotation function cannot be performed because Orientation cannot be identified */
MTextView01.setText (getResources (). getText
(R. string. str_err_1001 ));
}
Else
{
If (getRequestedOrientation () =
ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE)
{
/* If it is a horizontal column, change it to a vertical column */
SetRequestedOrientation
(ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );
}
Else if (getRequestedOrientation () =
ActivityInfo. SCREEN_ORIENTATION_PORTRAIT)
{
/* If it is a vertical column, change it to a horizontal column */
SetRequestedOrientation
(ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );
}
}
}
});
}
@ Override
Public void setRequestedOrientation (int requestedOrientation)
{
// TODO Auto-generated method stub
/* Determine the direction to be changed, with the 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
/* Override the getRequestedOrientation method to get the current screen direction */
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
Please note that the Android: screenOrientation attribute of the Activity needs to be set in AndroidManifest. xml. Otherwise, the program will not be able to determine the current Activity direction through the getRequestedOrientation () method.
<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 procedure, when the getRequestedOrientation () method is called to determine the click button, the display direction of the screen can be determined by the program, but the following method can be applied to mobile phones with different aspect ratios.
/* Method 2: Determine the screen aspect ratio */
Final Display defaultDisplay =
GetWindow (). getWindowManager (). getdefadisplay display ();
Int h = defaultDisplay. getHeight ();
Int w = defaultDisplay. getWidth ();
/* Click the current resolution as the resolution button */
MTextView01.setText
(Integer. toString (h) + "x" + Integer. toString (w ));
/If (w> h)
{
/* Landscape */
/* Override the setRequestedOrientation () method in the Activity */
SetRequestedOrientation
(ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );
}
Else
{
/* Portrait */
SetRequestedOrientation
(ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE );
}