Martin Belcher 26th May 2010

Intro

This article describes how to force the orientation of an Android view not to change ie screen not to rotate.


How to lock the orientation

In theonCreateDialog(int) eventof the activity use thesetRequestedOrientation(int) methodto set the screen orientation to your chosen setting. The activity will stay in this orientation regardless of if the device is tilted or not.

[Code sample – How to lock the orientation]
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

How to detect the current orientation

To programmatically detect the current orientation of the activity use the following code snippet. Theorientation propertyof theConfiguration classreturns three possible values corresponding to Landscape, Portrait and Square.

[Code sample – How to detect the current orientation]
switch(this.getResources().getConfiguration().orientation)
{
caseConfiguration.ORIENTATION_PORTRAIT:
//Dosomethinghere
break;
caseConfiguration.ORIENTATION_LANDSCAPE:
//Dosomethinghere
break;
caseConfiguration.ORIENTATION_SQUARE:
//Dosomethinghere
break;
default:
thrownewException("Unexpectedorientationenumerationreturned");
break;
}

Example : Locking rotation while performing an action.

You might wish to disable the screen rotation whilst performing an action or by user command, to do this you need to combine the above samples to detect the current orientation and lock the display to that orientation.

[Code sample – Locking rotation while performing an action]
//Setsscreenrotationasfixedtocurrentrotationsetting
privatevoidmLockScreenRotation()
{
//Stopthescreenorientationchangingduringanevent
switch(this.getResources().getConfiguration().orientation)
{
caseConfiguration.ORIENTATION_PORTRAIT:
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
caseConfiguration.ORIENTATION_LANDSCAPE:
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}

Once your action has completed you may wish to enable screen rotation again, see the next section for an example on how to do this.


How to re-enable screen rotation

To enable the orientation to be automatically changed on device tilt simply pass thesetRequestedOrientation(int) methodthe enumeration value for an unspecified orientation.

[Code sample – How to re-enable screen rotation]
//allowscreenrotationsagain
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android Error:Could not open cp_proj re
  2. Android 8.0 app内覆盖安装
  3. Android设置或清除默认桌面
  4. Android Studio 第七十七期 - Android 广
  5. andoird HTTP 工具类
  6. [Android]自定义系统菜单的背景
  7. 在android中获取系统后台运行的进程
  8. Android之View的视图测量过程
  9. android友盟注意事项
  10. android快捷方式的创建与删除