Auto rotate doesn't work after setRequestedOrientation when button is clicked

Viewed 1228

I have this video view. It has a fullscreen button as a controller. The auto rotation works normally at first, but whenever the fullscreen button is clicked, the screen rotate to be landscape, and when I try to auto rotate, it doesn't auto rotate to portrait.

Here is a screenshot of my video view and controller.

enter image description here Here is my code for fullscreen button onClickEvent

mFullScreen.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                //To fullscreen
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                mFullScreen.setImageResource(R.drawable.fullscreen);
                toolbar.setVisibility(View.VISIBLE);
                updateMetadata(false);

            } else {

                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                getSupportActionBar().hide();
                mFullScreen.setImageResource(R.drawable.normal_screen);
                toolbar.setVisibility(View.INVISIBLE);
                updateMetadata(true);
            }
        }
    });

Here is my onConfiguration changed method

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    getSupportActionBar().show();
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getSupportActionBar().hide();
        toolbar.setVisibility(View.INVISIBLE);
        try {
            channelNameText.setVisibility(View.GONE);
            channelImage.setVisibility(View.GONE);
        } catch (NullPointerException ne) {
            ne.printStackTrace();
        }

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        }
        updateMetadata(false);

    } else {

        Logger.e("Orientation", "ORIENTATION_POTRAIT");

        try {
            channelNameText.setVisibility(View.VISIBLE);
            channelImage.setVisibility(View.VISIBLE);
        } catch (NullPointerException ne) {
            ne.printStackTrace();
        }

        mFullScreen.setImageResource(R.drawable.fullscreen);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        }
        updateMetadata(true);
    }
}

The onConfiguration method is called but only when we press the fullscreen button. How do I enable auto rotation after setRequestedOrientation?

1 Answers
Related