Changing Screen Orientation in android

China☆狼群 提交于 2019-12-11 08:29:16

问题


i want to change the Screen Orientation to SENSOR when the Frame Layout is visible (i.e video is playing in full screen) which is working fine , But i want to revert it back to PORTRAIT when the frame layout is gone Which is Not working (No Errors, but the code is not doing anything) i have tried this so far

// Finally show the custom view container.
        mCustomViewContainer.setVisibility(View.VISIBLE);
        mCustomViewContainer.bringToFront();

        // auto orientation change if video is playing
        if (mCustomViewContainer.getVisibility() == View.VISIBLE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
        } else {        
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }       
    }

the workaround i found was to change orientation onbackpressed because that's where i am hiding my frame layout also

@Override
public void onBackPressed() {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    exitdlg = (View) findViewById(R.id.exitdlg);
    options1 = (View) findViewById(R.id.options1);
    if (mCustomView != null) {// close full screen video view if open
        mWebChromeClient.onHideCustomView();// FULL-SCREEN VIDEO-5
    } else if (options1.getVisibility() == View.VISIBLE) {
        findViewById(R.id.options1).setVisibility(View.GONE);

    } else if (exitdlg.getVisibility() == View.GONE) {// this brings
                                                        // up/hides
        // menu 1 on hardware
        // options btn
        // Its visible
        findViewById(R.id.exitdlg).setVisibility(View.VISIBLE);
    } else {
        // Either gone or invisible
        findViewById(R.id.exitdlg).setVisibility(View.GONE);

    }
}

but it only works when i press back button Twice which i don't want. that's where i got this code

来源:https://stackoverflow.com/questions/28496961/changing-screen-orientation-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!