Basic Android tutorial-a simple example of dynamically changing the screen direction (landscape and portrait )!

Source: Internet
Author: User

Hello everyone, what I want to talk about today isAndroidHow can a mobile phone dynamically move the screen direction of the mobile phone? Maybe the mobile phone also has this function. When the direction of the mobile phone changes, the screen also changes.AndroidIt is easy to implement in this section.DemoThere is a button on the interface. When a button is clicked, if the screen direction is horizontal (Portrait) Just changed the screen direction to a vertical bar (Landscape), And vice versa! Here we mainly useGetrequestedorientation(), AndSetrequestedorientation() Two methods. But to use these two methods, you must firstAndroidmaniefst. xmlSet the screen property, or the program will not work normally. Next I will divide it into n steps to teach you how to implement this step by step.Demo.

 

Step 1:We createAndroidProject, namedChangeorientationdemo.

 

Step 2: DesignUI, OpenMain. xml, Modify the code as follows. Here we just add a button, nothing else.

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Button
Android: Id = "@ + ID/Bt1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "press me change orientation"
/>

</Linearlayout>

 

Step 3: Design the main programChangeorientationdemo. Java, Modify the Code as follows:

 

Package com. Android. test;

Import Android. App. activity;
Import Android. content. PM. activityinfo;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. Button;

Public class changeorientationdemo extends activity {

Private button Bt1;
 
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

// Obtain resources
Bt1 = (button) findviewbyid (R. Id. Bt1 );
// Add button event
Bt1.setonclicklistener (New button. onclicklistener (){

@ Override
Public void onclick (view v ){
// If it is a vertical bar, it is changed to a horizontal bar.
If (getrequestedorientation () = activityinfo. screen_orientation_landscape)
{
Setrequestedorientation (activityinfo. screen_orientation_portrait );
}

// If the row is horizontal, it is changed to vertical

Else if (getrequestedorientation () = activityinfo. screen_orientation_portrait)
{
Setrequestedorientation (activityinfo. screen_orientation_landscape );
}
}

});
}

}

 

Step 4: InAndroidmanifest. xmlSet the default direction in the file, or the program will not work properly. The Code is as follows:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Android. test"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". changeorientationdemo"
Android: Label = "@ string/app_name"
Android: screenorientation = "portrait">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>

</Application>
<Uses-SDK Android: minsdkversion = "3"/>

</Manifest>

 

Step 5:Run the program. The effect is as follows:

 

 

 

OK. It's here today. Thank you!

Related Article

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.