Monday, December 19, 2016
Code Snippet Restricting Orientations
Code Snippet Restricting Orientations
Recent week, I have been hard finding it to restrict orientation to portrait and reverse portrait only. Today, got a solution/workaround. Thought it would be useful to share it.
The concern is to restrict the screen orientation to portrait and reverse portrait. Landscape and reverse landscape should not be supported. Hope you know that setting the screen orientation for activity(screen) can be done in either manifest file or in activity.
There is an attribute called "sensorPortrait"which exactly solves our case, as given in Android Developers forum. The point is ofcourse, its there.Works as expected in many devices. But it doesnt work in either Galaxy S3,S4 or Nexus. There is even a bug raised in google site. The challenge was to fix the orientation in these devices.
Was trying to address the issue with setting the configchanges for orientation in manifest and trying to handle it in the onConfigurationChanged in activity. None of them worked out. Got insights from one of our old team-mate reg. (OrientationEventListener) and another app(The Ultimate Rotation Control) developer reg. (WindowOrientationListener).
Tried using the first one and referred the web and got a workaround.
PFB the workaround for my issue,
-------------------------------------------------------------------------------------------------------------
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Menu;
import android.view.OrientationEventListener;
import android.widget.Toast;
public class MainActivity extends Activity {
OrientationEventListener orientationListener;
private static final int THRESHOLD = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "On Create", Toast.LENGTH_SHORT).show();
orientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
public void onOrientationChanged(int orientation) {
if(isPortrait(orientation)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else if(isReversePortrait(orientation)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
orientationListener.disable();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
orientationListener.enable();
super.onResume();
}
private boolean isReversePortrait(int orientation){
return orientation >= (180 - THRESHOLD) && orientation <= (180 + THRESHOLD);
}
private boolean isPortrait(int orientation){
return (orientation >= (360 - THRESHOLD) && orientation <= 360) || (orientation >= 0 && orientation <= THRESHOLD);
}
public boolean canShow(int orientation){
return isReversePortrait(orientation);
}
public boolean canDismiss(int orientation){
return isPortrait(orientation);
}
}
The values, (180, 360) are for portrait and reverse portrait modes. For Landscape and reverse landscape, you can try up with 90 and 270, where THRESHOLD is nothing but the limit within which the device can be considered as rotated to that position.Example - (360+-THRESHOLD) is portrait.
I have attached the full class file for reference.
References
http://stackoverflow.com/questions/8248274/android-detect-orientation-changed
The concern is to restrict the screen orientation to portrait and reverse portrait. Landscape and reverse landscape should not be supported. Hope you know that setting the screen orientation for activity(screen) can be done in either manifest file or in activity.
There is an attribute called "sensorPortrait"which exactly solves our case, as given in Android Developers forum. The point is ofcourse, its there.Works as expected in many devices. But it doesnt work in either Galaxy S3,S4 or Nexus. There is even a bug raised in google site. The challenge was to fix the orientation in these devices.
Was trying to address the issue with setting the configchanges for orientation in manifest and trying to handle it in the onConfigurationChanged in activity. None of them worked out. Got insights from one of our old team-mate reg. (OrientationEventListener) and another app(The Ultimate Rotation Control) developer reg. (WindowOrientationListener).
Tried using the first one and referred the web and got a workaround.
PFB the workaround for my issue,
-------------------------------------------------------------------------------------------------------------
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Menu;
import android.view.OrientationEventListener;
import android.widget.Toast;
public class MainActivity extends Activity {
OrientationEventListener orientationListener;
private static final int THRESHOLD = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, "On Create", Toast.LENGTH_SHORT).show();
orientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
public void onOrientationChanged(int orientation) {
if(isPortrait(orientation)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}else if(isReversePortrait(orientation)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
}
};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
orientationListener.disable();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
orientationListener.enable();
super.onResume();
}
private boolean isReversePortrait(int orientation){
return orientation >= (180 - THRESHOLD) && orientation <= (180 + THRESHOLD);
}
private boolean isPortrait(int orientation){
return (orientation >= (360 - THRESHOLD) && orientation <= 360) || (orientation >= 0 && orientation <= THRESHOLD);
}
public boolean canShow(int orientation){
return isReversePortrait(orientation);
}
public boolean canDismiss(int orientation){
return isPortrait(orientation);
}
}
-------------------------------------------------------------------------------------------------------------
The values, (180, 360) are for portrait and reverse portrait modes. For Landscape and reverse landscape, you can try up with 90 and 270, where THRESHOLD is nothing but the limit within which the device can be considered as rotated to that position.Example - (360+-THRESHOLD) is portrait.
I have attached the full class file for reference.
References
http://stackoverflow.com/questions/8248274/android-detect-orientation-changed
Available link for download
Labels:
code,
orientations,
restricting,
snippet