Android Development--teaches you how to handle screen rotation skillfully

Source: Internet
Author: User

Android Phone Support screen rotation function, mobile phone screen rotation directly triggered our app view changes, we choose how to handle the mobile phone flip and when our mobile phone screen has been rotated, we choose which strategy to process the view directly affect the user experience of our app. Today we will learn about mobile screen rotation.

Set the properties of the screen orientation toggle

In order for the activity to adapt to different situations in the screen rotation direction, the Android:screenorientation property is provided under the Activity tab of the Androidmanifest file to meet different screen rotation requirements.

    • Unspecified: Default value, this property supports the screen rotation direction associated with a specific device;
    • User: The current preferred direction of users, usually with the same effect as unspecified;
    • Behind: The same direction as the current activity in the fallback stack of the following window;
    • Landspace: Make the screen always keep horizontal screen, the phone normal direction counterclockwise rotation 90 degrees;
    • Portrait: Make the screen always keep the vertical screen, that is, the normal direction of the phone;
    • Reverselandscape: Division screen always keep the reverse horizontal screen, the phone normal direction clockwise rotation of 90 degrees;
    • Sensorlandscape: The system will use the sensor of the Android device to switch to the horizontal screen or anti-Shang;
    • Sensorportrait: The system will use the sensor of the Android device to switch to vertical screen or reverse vertical screen;
    • Sensor: The system will use the Android device to switch to the corresponding direction, in theory, 4, but many devices only support 3;
    • Fullsensor: Enables the system to support 4-direction screen rotation;
    • Nosensor: The rotation of the screen does not occur, but the destruction and rebuilding of the activity may occur (same as the unspecified policy).
Prevent the destruction and reconstruction of activity

By default, when our phone screen spins, the current activity is destroyed, and then the activity is recreated, which is obviously a bit superfluous when we don't need to change the layout when the screen is rotated (and later on how to change the layout when the screen rotates). Android enables developers to set up android:configchanges under the Activity tab to handle settings changes for Android devices.

Under API 12 (i.e. Android 3.1) We need to set android:configchanges= "orientation" to prevent the destruction and reconstruction of the activity, above the API 12 we need to set up Android: Configchanges= "Orientation|screensize"

Change the layout to get a good user experience

Because Android devices are generally different in length and height, when the screen orientation of our phone changes, if the previous layout may degrade the user experience, the following describes how to change the layout when the screen rotates. When my phone is on a horizontal screen, Android will find and use the layout file in the Res/layout-land directory so that we know how to handle it. To give a simple example, we have a row of tabs at the top of our phone screen when the phone is vertical, and when the phone flips, put the Tab tab to the left to get a good user experience:

Create a new layout file in the Res/layout directory under Android engineering Activity_main.xml

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:orientation="Vertical"tools:context=". Mainactivity "> <radiogroup android:ID="@+id/main_radiogroup"Android:layout_width="Match_parent"android:layout_height="Wrap_content"android:orientation="Horizontal"> <radiobutton android:ID="@+id/main_radio0"Android:checked="true"Android:text="Showone"style="@style/radio_style"/> <radiobutton android:ID="@+id/main_radio1"Android:text="Showtwo"style="@style/radio_style"/> <radiobutton android:ID="@+id/main_radio2"Android:text="Showthree"style="@style/radio_style"/> </RadioGroup> <textview android:ID="@+id/textview1"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:gravity="Center"Android:textsize="20SP"Android:text="TextView"/></linearlayout>

Next, create the Activity_main.xml file under the Res/layout-land directory (which can be created directly without the directory), with the following layout:

<linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:orientation="Horizontal"tools:context=". Mainactivity "> <radiogroup android:ID="@+id/main_radiogroup"Android:layout_width="Wrap_content"android:layout_height="Match_parent"android:orientation="Vertical"> <radiobutton android:ID="@+id/main_radio0"Android:checked="true"Android:text="Showone"Android:layout_width="100DP"style="@style/radio_style"/> <radiobutton android:ID="@+id/main_radio1"Android:text="Showtwo"Android:layout_width="100DP"style="@style/radio_style"/> <radiobutton android:ID="@+id/main_radio2"Android:text="Showthree"Android:layout_width="100DP"style="@style/radio_style"/> </RadioGroup> <textview android:ID="@+id/textview1"Android:layout_width="Match_parent"android:layout_height="Match_parent"android:gravity="Center"Android:textsize="20SP"Android:text="TextView"/></linearlayout>

It is important to note that we cannot prevent the activity from being destroyed or rebuilt, otherwise the layout will not change.

Save Activity status

To prevent the activity from losing data during rotation, we can save the data before the device spins:

    1. Overwrite the onsaveinstancestate to save our data under the bundle;
    2. Getting data in the OnCreate method
 PackageCom.example.rotatetest;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Menu;ImportAndroid.widget.RadioButton;ImportAndroid.widget.RadioGroup;ImportAndroid.widget.RadioGroup.OnCheckedChangeListener;ImportAndroid.widget.TextView; Public  class mainactivity extends Activity {    PrivateRadiogroup Mradiogroup;PrivateTextView Mtextview;Private Static FinalString Last_choice ="Last_choice";@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Mradiogroup = (radiogroup) Findviewbyid (R.id.main_radiogroup); Mtextview = (TextView) Findviewbyid (R.ID.TEXTVIEW1);//Get the ID of the RadioButton selected before destruction        intCheckid = R.id.main_radio0;if(Savedinstancestate! =NULL) {Checkid = Savedinstancestate.getint (Last_choice); } ((RadioButton) Findviewbyid (Checkid)). setchecked (true);Switch(Mradiogroup.getcheckedradiobuttonid ()) { CaseR.id.main_radio0:mtextview.settext ("Show One"); Break; CaseR.id.main_radio1:mtextview.settext ("Show "); Break; CaseR.id.main_radio2:mtextview.settext ("Show three"); Break;default: Break; } mradiogroup.setoncheckedchangelistener (NewOncheckedchangelistener () {@Override             Public void oncheckedchanged(Radiogroup Group,intCheckedid) {//TODO auto-generated method stub                Switch(Checkedid) { CaseR.id.main_radio0:mtextview.settext ("Show One"); Break; CaseR.id.main_radio1:mtextview.settext ("Show "); Break; CaseR.id.main_radio2:mtextview.settext ("Show three"); Break;default: Break;    }            }        }); }@Override    protected void onsaveinstancestate(Bundle outstate) {//TODO auto-generated method stub        Super. Onsaveinstancestate (Outstate);//Save the ID of the selected RadioButtonOutstate.putint (Last_choice, Mradiogroup.getcheckedradiobuttonid ()); }}
Monitor screen rotation events

Sometimes we want to be notified when the device is spinning so we can take care of things, and see how to capture the notification. The screen orientation changes the settings of the Android device, which we can get from the Onconfigurationchanged method.

    1. Declare the event type android:configchanges= "Orientation|screensize" in the Androidmanifest file that the activity is to be captured
    2. Rewrite the Onconfigurationchanged method in activity
@Override public void onconfigurationchanged (Configuration newconfig) {//TODO auto-generated method stub Super. onconfigurationchanged(Newconfig);if (newconfig. Orientation= = Configuration. ORIENTATION_portrait) {Toast. Maketext(mainactivity. this,"Now is the vertical screen", Toast. LENGTH_short). Show();} if (Newconfig. Orientation= = Configuration. ORIENTATION_landscape) {Toast. Maketext(mainactivity. this,"Now is the horizontal screen", Toast. LENGTH_short). Show();}    }

Test Source Download

Click to download the source code

Android Development--teaches you how to handle screen rotation skillfully

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.