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
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 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
Does anyone know how to disable Orientation change while recording ?
I tried something like this, but no success. any ideas?
#Override
public void onConfigurationChanged(Configuration newConfig) {
if (!recordingAudio && !recordingVideo) {
super.onConfigurationChanged(newConfig);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
I've done it by adding the following in the manifest file as an attribute of the activity tag:
android:screenOrientation="nosensor"
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