-4

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);

476rick
  • 2,764
  • 4
  • 29
  • 49
  • have you added `configChanges` in your manifest for the activity running the video? – not_again_stackoverflow Sep 22 '16 at 11:46
  • Yes my Manifest file has this " android:configChanges="orientation|screenSize|keyboardHidden"" – Arunabha Dutta Choudhury Sep 22 '16 at 11:49
  • avoid posting just your problem without any code – Rahul Khurana Sep 22 '16 at 11:49
  • 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); – Arunabha Dutta Choudhury Sep 22 '16 at 12:57
  • Once you invoke this statement setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENS‌​OR_LANDSCAPE); the screen is not rotating back to potrait mode even if i rotate device.The activity remains in Landscape mode only.I tried the solution you advise but it seems to have no impact – Arunabha Dutta Choudhury Sep 22 '16 at 12:59
  • In a nutshell i am trying to replicate the rotation behavior of Youtube/Hotstar player – Arunabha Dutta Choudhury Sep 23 '16 at 06:06
  • Finally answer by @Znat helped me get the desired result.Here is the link in case anyone needs solution for similar problem. http://stackoverflow.com/questions/13073500/onconfigurationchanged-not-called-once-setrequestedconfiguration-has-been-used – Arunabha Dutta Choudhury Sep 23 '16 at 09:27

1 Answers1

0

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" />
Community
  • 1
  • 1
476rick
  • 2,764
  • 4
  • 29
  • 49