In some scenarios, we need to change the screen direction of the mobile phone, here is a sample I wrote.
XML Paging File:
< Relativelayout 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:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=". Mainactivity ">
<Button
android:id="@+id/btn"
android:layout_width="Wrap_content"
android:layout_height="Wrap_content"
android:layout_alignparentleft="true"
android:layout_alignparenttop="true"
Android:text="Change screen orientation"/>
</ Relativelayout>
Page Java files:
Package com.example.chcfg;
import Android.os.Bundle;
import android.app.Activity;
import Android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import Android.view.View;
import Android.view.View.OnClickListener;
import Android.widget.Button;
import android.widget.Toast;
Public class Mainactivityextends Activity {
@Override
protected void onCreate (Bundle savedinstancestate) {
Super. OnCreate (savedinstancestate);
Setcontentview (r.layout. Activity_main);
Button btn = (button) Findviewbyid (r.id. BTN);
Btn.setonclicklistener (new Onclicklistener ()
{
Public void OnClick (View source) {
TODO auto-generated Method stub
Configuration config = getresources (). GetConfiguration ();
if (config. Orientation = = Configuration. Orientation_landscape)
{
Set as vertical screen
Mainactivity. this. Setrequestedorientation (activityinfo. Screen_orientation_portrait);
}
Assuming the current is a vertical screen
if (config. orientation = = Configuration. orientation_portrait)
{
Mainactivity. this. Setrequestedorientation (activityinfo. Screen_orientation_landscape);
}
}
});
}
@Override
Public void onconfigurationchanged (Configuration newconfig) {
TODO auto-generated Method stub
Super. onconfigurationchanged (Newconfig);
String screen = Newconfig. orientation = = Configuration. orientation_landscape ? "Landscape Screen" : "Portrait Screen";
Toast.maketext (This,"the system screen orientation has changed \ nthe direction is:" + screens , Toast. ) Length_long). Show ();
}
}
Mainfest XML file:
<? XML Version="1.0"encoding="utf-8"?
>
< Manifest xmlns:android="Http://schemas.android.com/apk/res/android"
Package="Com.example.chcfg"
Android:versioncode="1"
android:versionname="1.0">
<USES-SDK
android:minsdkversion="8"
android:targetsdkversion="/> "
<uses-permissionandroid:name=" Android.permission.CHANGE_CONFIGURATION "/>
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme">
< Activity
android:name="Com.example.chcfg.MainActivity"
android:label="@string/app_name"
android:configchanges="orientation">
<intent-filter>
<actionandroid:name="Android.intent.action.MAIN" c9>/>
<categoryandroid:name="android.intent . category. LAUNCHER "/>
</intent-filter>
</ Activity >
</application>
</manifest>
Personal hard work results, if reproduced. Please indicate the source. Thank you.
Android Development Change screen orientation