I have a live wallpaper in the market that runs very well for potrait mode, but as soon as its in landscape.. it looks stretched and bad. I have developed the wallpaper in andengine. I looked through the web to figure out that i need to implement onConfigurationChanged() method to take care of orientation changes. I used the solution proposed here :-
http://www.andengine.org/forums/live-wallpaper-extension/orentation-problem-landscape-portrait-t10669.html
Following is my onConfigurationChanged() method
#Override
public void onConfigurationChanged(Configuration newConfig) {
if (MODE == 0) {
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
mMainScene.setScale(1);
mMainScene.setPosition(0, 0);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
mMainScene.setScaleY(1.5f);
mMainScene.setScaleX(0.5f);
mMainScene.setPosition(260, -500);
}
} else if (MODE == 1) {
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
mMainScene.setScaleY(0.5f);
mMainScene.setScaleX(1.5f);
mMainScene.setPosition(-500, 250);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
mMainScene.setScale(1);
mMainScene.setPosition(0, 0);
}
}
}
The problem is that now when i see my wallpaper in preview mode, my mMainScene gets shifted to the right by 260 pixels as i have specified in my onConfigurationChanged() method. I think this problem can be solved if i can detect when my wallpaper is in preview mode but i can't seem to find out how to? I did try BaseWallpaperGLEngine.isPreview() method but it gives me a nullpointer exception. Can someone help me?
you can handle orientation change more properly, and avoid problems in preview mode. Look at this answer: AndEngine portrait distortion with RotationModifier
Related
With
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
I can find the orientation of my layout. Now I want to lock my layout in some orientation e.g setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); and to find real phone orientation (layout is set to portrait, but user for some reason holds his device in landscape)
You have 2 options.
Use config changes:
Manifiest
<activity android:name=".HelloAndroid"
android:label="#string/app_name"
android:configChanges="orientation">
In your code
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
// TODO
}
You have to override this method.
Use an OrientationEventListener, which has a method called onOrientationChanged.
Here is a good Handling Runtime Changes tutorial.
EDIT:
Try this:
if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
// portrait mode
} else if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
// landscape
}
I have tested this way and it works for me.
I am playing a video and it has got a fullscreen icon.On clicking on it i am forcefully rotating the device orientation to landscape mode.But then when i rotate the screen,the video is locked into landscape mode only.It is not coming back to potrait mode.I want the video rotation to happen similar to Youtube/Hotstar app,where change of rotation happens both on button click and device orientation change.Kindly suggest some way forward.
findViewById(R.id.landscape).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
}
});
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setAutoOrientationEnabled(this,false);
}
Question:
My simple requirement is after forcefully rotating the activity using setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); how can i again go back to potrait mode by rotating the device without calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_POTRAIT);
For the button you can just change it by using this lines :
Button buttonSetPortrait = (Button)findViewById(R.id.setPortrait);
Button buttonSetLandscape = (Button)findViewById(R.id.setLandscape);
buttonSetPortrait.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
buttonSetLandscape.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
Answer from : https://stackoverflow.com/a/18268304
Use the following code set the videos orientation when the device orientation changes
Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
Take a look at this answer : https://stackoverflow.com/a/5726776
For enable or disable auto rotate take a look at this answer: (https://stackoverflow.com/a/4909079/4585226)
import android.provider.Settings;
public static void setAutoOrientationEnabled(Context context, boolean enabled)
{
Settings.System.putInt( context.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, enabled ? 1 : 0);
}
Add permission to the AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
I need help.
My app still fails when the device orientation changes to landscape.
Whats the problem?
I have set in the Android manifest android:configChanges="keyboardHidden|orientation"
and in the main Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
setContentView(R.layout.main);
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
and my activities are fragments.
Improper onConfigurationChanged().
Remove setContentView(R.layout.main); from onConfigurationChanged().
Probably you are having the problem when your activity is being recreated.
Perhaps the solution might be here
Is there any way to temporarily stop rotating the screen, perform a restore operation?
Request can not be turned off gravity sensor, it only prohibits the rotating screen.
To disable the orientation change you need to tell Android that you want to handle it by yourself.
To do so, add the following to your activity in the mainfest.xml:
android:configChanges="orientation"
Now override the following in the Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
If you don't load a new layout here you will just keep the orientation.
Further details you can find here: http://developer.android.com/guide/topics/resources/runtime-changes.html
I am implementing a camera in android. I have kept the activity as landscape in the manifest.
Since i have given the orientation as fixed, i am not able to get orientation by display. it always gives as LandScape. But i want to know when my device is held in portrait or vertical position. I do not want the screen orientation. Can any one suggest a good way to detect device orientation.
Thanks all
I think you will have to listen to the accelerometer sensor updates and parse them to determine when the orientation changes. there is some examples of listening to the sensors here: http://www.anddev.org/accessing_the_accelerometer-t499.html and here http://mobilestrategist.blogspot.com/2010/01/android-accelerometer-and-orientation.html
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
// Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
// Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {
// Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
To detect screen orientation you can use the following code in your activity
#Override
public void onConfigurationChanged(Configuration newConfig)
{ super.onConfigurationChanged(newConfig);
}
Thanks
Deepak
Try this:
First implement SensorEventListener and get the RotationSensor
sensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE);
rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
sensorManager.registerListener(this, rotationSensor, SENSOR_INTERVAL);
int FROM_RADS_TO_DEGS = -57;
Then you can detect the angle of the device like this:
#Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor == rotationSensor) {
if (event.values.length > 4) {
float[] truncatedRotationVector = new float[4];
System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
updateRotation(truncatedRotationVector);
} else {
updateRotation(event.values);
}
}
}
private void updateRotation(float[] vectors) {
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
int worldAxisX = SensorManager.AXIS_X;
int worldAxisZ = SensorManager.AXIS_Z;
float[] adjustedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
float[] orientation = new float[3];
SensorManager.getOrientation(adjustedRotationMatrix, orientation);
float pitch = orientation[1] * FROM_RADS_TO_DEGS;
if(pitch < -45 && pitch > -135) {
// if device is laid flat on a surface, we don't want to change the orientation
return;
}
float roll = Math.abs(orientation[2] * FROM_RADS_TO_DEGS);
if((roll > 45 && roll < 135)) {
// The device is closer to landscape orientation. Enable fullscreen
if(!player.isFullScreen()) {
if(getActivity() != null) {
player.setFullScreenOn();
}
}
}
else {
// The device is closer to portrait orientation. Disable fullscreen
if(player.isFullScreen()) {
if(getActivity() != null) {
player.setFullScreenOff();
}
}
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do nothing
}
This got the original code from a tutorial, but it was a while ago, so I cant remember where the tutorial was. This version is heavily customised to my requirements, but if anyone recognises it from the original, please drop in the link.
I used this code to detect when a video player should go fullscreen inside a ViewPage that I didn't want to allow landscape orientation on. It works well except for one thing:
It uses RotationSensor hardware, and not all Android devices have a RotationSensor. If anyone knows of a way to do it using some hardware that is included on all devices (There definitely is a way because Android knows when to switch orientation), please let me know in a comment so I can update my own code.
I use this:
#Override
public void onConfigurationChanged(Configuration _newConfig){
super.onConfigurationChanged(_newConfig);
int height = getWindowManager().getDefaultDisplay().getHeight();
int width = getWindowManager().getDefaultDisplay().getWidth();
if(width > height){
// landscape
}else{
// portrait
}
}
It is crude but effective.