Disable Orientation Change while recording Video - android

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"

Related

How to to find if device orientation is same as screen orientration

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.

Screen rotation while playing video

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_SENS‌​OR_LANDSCAPE); how can i again go back to potrait mode by rotating the device without calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENS‌​OR_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" />

Android videoview doesn't play in landscape mode

I have an activity that play a video with VideoView. Video starting in onCreate, and starts only if layout is in portrait mode, after it has started i can change to landscape and video continue to playing correctly.
The problem is when starting activity in landscape mode, in this case video doesn't play.
AndroidManifest.xml
<activity
android:name="...."
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_fullscreen_rec"
android:launchMode="singleTop"
android:parentActivityName="...." >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="...." />
</activity>
In onCreate method I simply set videosource and start videoview.
Then I have:
#Override
#SuppressLint("NewApi")
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Add this code after the onCreate Method in your activity.
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}
please check this. tell me if this works ?

Change orientation ANDROID

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

Android App How to rotation screen orientation temporarily

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

Categories

Resources