vxhviet
4/28/2016 - 11:13 AM

Rotate screen on button click

Rotate screen on button click

Source: StackOverflow, TechBlogon

Question: How to rotate screen orientation on button click?

Answer:

In VideoActivity:

mFullscreen.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(UIUtil.checkRotation(VideoActivity.this) == Surface.ROTATION_90
                            || UIUtil.checkRotation(VideoActivity.this) == Surface.ROTATION_270){
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    }else{
                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    }
                }
            });

In UIUtil:

public static int checkRotation(Context ctx){
        Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int orientation = display.getRotation();
        return orientation;
    }

In AndroidManifest.xml

<activity
            android:name=".vietactivities.VideoActivity"
            android:screenOrientation="portrait"/>