Locking ScreenOrientation - android

I am making an App which allow you read Wikipedia pages.
I want to display an icon each time when is phone is rotated from the portrait to landscape or vice versa. It will let user lock the screen orientation if he wants to or else the screen is oriented according to the sensor data. This is functionality is implemented by some of the App in the google play, for example - Pocket
To do this is i have overridden the
public void onConfigurationChanged(Configuration newConfig)
Now if i am setting the locked configuration by using( orientation_dir is the orientation value stored in the shared preferences and it is correct as i have debugged through the code for it.)
if(orientation_dir == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else if(orientation_dir == Configuration.ORIENTATION_PORTRAIT)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
then orientation is set correctly but then onConfigurationChanged() method is not called when the phone is rotated.
If i set the orientation this way
if(orientation_dir == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else if(orientation_dir == Configuration.ORIENTATION_PORTRAIT)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
then desired orientation is not set. Phone reset the orientation according to the sensor data.
I tried even by not calling the super in the method as i thought it might be setting it wrong but then it gives me the exception "Super not called".
I am trying for last 2 days and haven't got a any solution to this problem.

In general onConfigurationChanged() event fire only when an configuration change has occured. In your app the orientation changed event occurs only when the screen is free to rotate. If you have locked the screen orientation then the screen orientation event is not fired. onConfigurationChanged() does not listen to the sensor that is responsible to rotate the device but fires only when the appropriate event is happening.
So what you really want is to have access to a SensorManager and attach a SensorListener. This is the way for you to listen to the actual orientation of the device.
Here is a very nice demo that demonstrates the SensorManager capabilities with the orientation of the phone:
http://www.workingfromhere.com/blog/2009/03/30/orientation-sensor-tips-in-android/
UPDATE: The orientation sensor is a composite sensor to make things easier for the developer. It does not actually exist in the phone. Is a very neat sensor combining the accelometer sensor and the magnetic field sensor. And the OrientationSensor is currently deprecetated according to the docs (http://developer.android.com/guide/topics/sensors/sensors_position.html).
See What is the alternative to android orientation sensor? for sample usage.
it may need some fixing I have not tested it much.

onConfigurationChanged - is a notification to let your activity know the change in orientation.
You should not attempt to change or fix the orientation in that method. If you set the orientation to landscape or portrait then you will not get new orientation change notification because the orientation of the activity does not change. May be you should set the orientation setting in the event handler of the widget that you plan to show when there is a change in orientation.

Related

How to get current moment Activity status in portrait or landscape, not the orientation setting

I write a fragment. By it's UI, it only good when activity is current in portrait (or reversed) status.
I know activity can be set as:
portrait or reverse portrait mode
user mode or sensor mode
un-specified mode.
All of above setting may lead to a portrait status in some case.
I want do:
check if activity is current in portrait status, but not care if it's portrait mode or user mode or un-specified mode, also no care if the device is rotated or not. I just care the activity status in just the current moment, because of the UI.
if current is portrait, than save activity orientation setting, and set it as lock setting(ActivityInfo.SCREEN_ORIENTATION_LOCKED).
show the fragment UI, and user close it.
restore activity orientation setting.
Now I can do step 2/3/4, but I don't how to do step 1.
I have read a lot of post in stackoverflow, but most of posts are about orientation setting, not the current moment status.
I had know how to check rotation in activity or in fragment such as onConfigurationChanged. But I hope check first, and then run my fragment. I also hope not insert code in activity to monitor rotation.
Please tell me how to do step 1. Thanks.
==================================
So sorry to post such a stupid question. It's very simple --- just check content view's height and width.
public boolean isPortraitStatus(Activity activity){
View contentView = activity.findViewById(android.R.id.content);
int height = contentView.getHeight();
int width = contentView.getWidth();
if (height > width){
return true;
}else{
return false;
}
}
if I should delete this post?

onConfigurationChanged() calling twice when change orientation Landscape to Portrait

i am getting strange issue in my app. The main issue that i have asked here. issue is android:configChanges="keyboardHidden|orientation" is not working in my code.
so i have found the solution to manage it by #Override onConfigurationChanged() method in my code to manage orientation. but yet the issue is not solve properly.
Currently issue is that onConfigurationChanged() is calling twice when we change orientation
landscape to portrait.
If we change phone portrait to landscape its changing and working but now when user move the phone landscape to portrait then onConfigurationChanged() will call and return same orientation state & in second call it will return portrait.
Code :
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Log.e("On Config Change", "LANDSCAPE");
Toast.makeText(getApplicationContext(), "L", Toast.LENGTH_LONG)
.show();
} else
{
Log.e("On Config Change", "PORTRAIT");
Toast.makeText(getApplicationContext(), "P", Toast.LENGTH_LONG)
.show();
}
}
Log
first mode its port mode , so change in land mode
02-28 12:10:06.274: E/On Config Change(540): LANDSCAPE
02-28 12:10:14.154: E/On Config Change(540): LANDSCAPE
// here after changed the land mode try to chage in port mode then its calling two times as you can see as per the log
02-28 12:10:14.593: E/On Config Change(540): PORTRAIT
02-28 12:11:39.524: E/On Config Change(540): LANDSCAPE
One more query with same question >>
It will kill the current activity when we change the orientation (at time of calling onConfigurationChanged). so i have two layouts in different folder as per my previous question.So when i change the screen activity will remove all data.so how can i save that data to show user when user change the phone orientation in any case.
I am not sure how onConfigurationChanged() gets called twice, but to avoid buggy behavior in your app you can void the execution of onConfigurationChanged() if the orientation received now is same as the previous orientation.
Worked for me, hope it works for you too :)

It is possible to known when the screen orientation is changed but with portrait mode forced on manifest?

Welcome all
I have a special need for my app. I must force the app to work only in portrait mode, but i need to know when the user has moved the phone to landscape mode. Why? because i am displaying a opengl view with a texture image, and when the user changues the phone position to landscape mode i must rotate the polygon without reseting the activity. Then i must force portrait mode on manifest, because i dont want that my onCreate method gets called again.
Please, can someone tell me how to achieve this?
I know how to rotate the image, i only need to know when the user has moved the phone to landscape position, but with portrait forced on manifest.
I tryed adding android:screenOrientation="portrait" in the manifest and also adding android:configChanges="keyboardHidden|orientation" to the declaration in the manifest; and then overriding onConfigurationChanged() in my activity. But this doens't works, because portrait mode is forzed, then onConfigurationChanged method is never called......
Thanks
Why not use the SensorManager to monitor when the phone has rotated 90 degrees. Actually this may be helpful also.
use this attribute in manifest android:configChanges="orientation" this stops recreating activity then override the onConfigurationChange() method. Give the same layout both in portrait and landscape mode. when orientation change occurs, that method wil be called then change the image as u like
Do Not set the screen orientation in the manifest (android:screenOrientation="portrait"). Add the android:configChanges="keyboardHidden|orientation", and override your onConfigurationChanged(). This way your onCreate will not be called twice.
Here a example of your onConfigurationChanged:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); // tem que ter
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}
}

Android - Lock orientation to two directions only

How can make my android application to be Landscape or portrait but without the reverse directions?
Meaning that I want to enable SCREEN_ORIENTATION_PORTRAIT and SCREEN_ORIENTATION_LANDSCAPE but to disable SCREEN_ORIENTATION_REVERSE_LANDSCAPE and SCREEN_ORIENTATION_REVERSE_PORTRAIT.
Thanks
I had the same problem/feature request in my camera app, and found this post:
android camera surfaceview orientation
In changeRotation just comment ORIENTATION_PORTRAIT_INVERTED and ORIENTATION_LANDSCAPE_INVERTED, and create a method rotateViews(int degrees) to rotate all of the images(buttons) of your layout when switching between regular landscape/portrait.
Also,
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
worked better for me than
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR | ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Make sure you notify the system that you want to handle orientation changes by adding "orientation" to the android:configChanges flag attribute in the activity tag in your manifest file.
Override Activity.onConfigurationChanged() and inspect the Configuration.orientation field in the passed Configuration parameter and set the orientation of the activity to either ActivityInfo.SCREEN_ORIENTATION_PORTRAIT or ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE using Activity.setRequestedOrientation appropriately.

Handling activity rotating in Android

I need to apply different layouts for portrait and landscape orientations of my activity. Besides, I need to show alert if orientation is portrait.
I have specified android:configChanges="orientation|keyboardHidden" in AndroidManifest. I also override onConfigurationChanged method like this:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
Log.d("tag", "config changed");
super.onConfigurationChanged(newConfig);
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT)
Log.d("tag", "Portrait");
else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
Log.d("tag", "Landscape");
else
Log.w("tag", "other: " + orientation);
....
}
While rotating from landscape to portrait log looks like:
config changed
Portrait
But while changing from portrait to landscape it looks like
config changed
Portrait
config changed
Landscape
Why onConfigurationChanged is called twice? How can I avoid it?
See my answer to another question here: https://stackoverflow.com/a/3252547/338479
In short, handling configuration changes correctly is hard to do. It's best to implement onRetainNonConfigurationInstance() which is called just before your application is about to be stopped and restarted due to a configuration change. Use this method to save anything you want ('this' is a good choice) and then let the system tear down your app.
When your app gets restarted with the new configuration, use getLastNonConfigurationInstance() to retrieve the state you just saved, and use it to continue your application without all that mucking about with bundles and shared preferences.
You can simply save the previous orientation and check if it has really changed.
If you set in AndroidManifest.xml android:configChanges to keyboardHidden|orientation for your activity, onCreate etc... won't be called. That makes the implementation significantly easier to implement. But of course layout will change from portrait to landscape as well.
Is there any particular reason you chose to handle rotation in this manner? While it is quicker since the activity doesn't get restarted on an orientation change, it isn't typically recommended, if I recall correctly. Another way to handle orientation changes is instead of overriding onConfigurationChanged(), overriding onCreate(), onStart() or onResume() such that
#Override
public void onStart() {
super.onStart();
int orientation = getWindowManager().getDefaultDisplay().getOrientation();
if(orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.i(TAG, "Orientation is portrait");
// show whatever alerts here
}
}
and then specifying two layouts - one for portrait, one for landscape. The portrait version of the layout would remain at res/layout/whatever.xml, and the landscape version would live in res/layout-land/whatever.xml. The AndroidGuys had written a bunch of good articles on this topic, see http://androidguys.com/?s=rotational+forces&x=9&y=9
I'm pretty sure you would want to use onCreate rather than onStart. The only difference appears to be that onStart will get called when the application comes to the foreground. That wouldn't be a case where you'd want to make the user wait for you to re-initialize the UI. Otherwise, just change your setContentView call based on that if condition.
Android starts a new instance of your activity when changing orientation so using onCreate is the ideal method. You will have to save/restore your activity's data obviously in order to pick up where you left off - but you should be doing this anyway since any number of events can unfocus/kill your application.

Categories

Resources