Reputation: 2592
I need to play the embedded videos in webview in normal orientation(as the webview is, such as PORTRAIT or LANDSCAPE), and force into LANDSCAPE mode in fullscreen video. So I did as the following steps:
android:configChanges="keyboardHidden|orientation|screenSize"
. in the WebView's WebChromeClient class,
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
LogUtil.d(TAG, "onShowCustomView");
super.onShowCustomView(view, callback);
mHandler.post(new Runnable(){
@Override
public void run()
{
Activity activity = getActivity();
if(activity == null){
return;
}
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
});
}
@Override
public void onHideCustomView()
{
super.onHideCustomView();
mHandler.post(new Runnable(){
@Override
public void run()
{
Activity activity = getActivity();
if(activity == null){
return;
}
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
});
}
;; After doing this, generally it works fine. But it has one fault: before playing the video in fullscreen, the activity/fragment can rotate according to the phone direction, but after i return from fullscreen(LANDSCAPE mode) to embedded(PORTRAIT mode), the activity/fragment never change its orientation. How can i handle this case? i just want to restore to the original state, not lock the orientation. Thanks
Upvotes: 0
Views: 1408
Reputation: 1110
Add this line after you set the orientation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
It isn't a perfect solution, but works pretty handy in a pinch.
android:configChanges
can be pretty dangerous, as it prevents the OS from doing its normal clean-up and recreation of activities.
Upvotes: 0
Reputation: 32271
Yes, you can control the screen orientation programmatically.
Upvotes: 0