Package com.example;
Import android.app.Activity;
Import Android.content.pm.ActivityInfo;
Import android.content.res.Configuration;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Changeconfiguationactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.configuration_layout);
Button btn = (button) Findviewbyid (R.ID.BN);
Btn.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Configuration config = getresources (). GetConfiguration ();
If it's a horizontal screen, set the vertical screen
if (config.orientation = = Configuration.orientation_landscape) {
Changeconfiguationactivity.this
. Setrequestedorientation (activityinfo.screen_orientation_portrait);
} else {
Changeconfiguationactivity.this
. Setrequestedorientation (Activityinfo.screen_orientation_landscape);
}
}
});
}
@Override
public void onconfigurationchanged (Configuration newconfig) {
Super.onconfigurationchanged (Newconfig);
String screen = Newconfig.orientation = = Configuration.orientation_landscape? "Landscape Screen"
: "Vertical screen";
Toast.maketext (Changeconfiguationactivity.this,
"The system screen orientation has changed:" + screens, Toast.length_long). Show ();
}
}
Androidmanifest.xml Configuration
<uses-permission android:name= "Android.permission.CHANGE_CONFIGURATION"/>
android:configchanges= "Orientation"
<activity
Android:name= ". XXX"
android:configchanges= "Orientation"
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Android Screen Detection