Nov 18, 2008

(Android) How to detect the rotation of screen

When the orientation of screen changed (portrait to landscape or reciprocate action), the current Activity will be paused->stopped->destroyed and then created->started->resumed with the new configuration (such as language changed, orientation changed..). It means we can detect the orientation on the OnCreate method, to get the current orientation, here is an example:


if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.i("info", "landscape");
}
else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i("info", "portrait");
}


So you can load the diffrent layout xml file to change your UI or change the layout parameter directly in the code.

No comments: