Android screen rotation not taking effect

423 Views Asked by At

May I check how can I rotate screen via code in Android?
The device (Android 6 ARM CPU) I am using does not have sensor to detect orientation change and I need to do it manually.
The problem of this device is it will not save the setting, once I change the screen orientation via Setting menu.
The next time its power on, it will go back to default "Landscape".
I tried to use below codes (one at a time) to change it, it is not working (means screen no change).

Settings.System.putInt(contentResolver, Settings.System.USER_ROTATION, 1);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

On this device, there are two options (Screen rotation and External screen rotation).
I need to change both orientation to achieve what I want via Settings - Display option.
Either Kotlin or Java codes are Ok for me. Any advice?

enter image description here

enter image description here

enter image description here

1

There are 1 best solutions below

1
Mohammad Mirshahbazi On

You can't rotate android core from in you application, but you can create the custom layout in your project and controlled by your self like this :

first create custom layout :

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.FrameLayout;

public class URotateLayout extends FrameLayout{
    public static final String TAG = "URotateLayout";
    public static final int ORIENTATION_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    public static final int ORIENTATION_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    public static final int ORIENTATION_SENSOR_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
    public static final int ORIENTATION_SENSOR_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
    public static final int ORIENTATION_SENSOR = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
    public static final int ORIENTATION_LOCKED = ActivityInfo.SCREEN_ORIENTATION_LOCKED;

    private int mOrientation;
    private int mLastOrientation;

    private int mDefaultVideoContainerWidth;
    private int mDefaultVideoContainerHeight;

    private int mScreenWidth;
    private int mScreenHeight;

    public URotateLayout(Context context) {
        super(context);
    }

    public URotateLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public URotateLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void updateScreenWidthAndHeight() {
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
        mScreenWidth = metrics.widthPixels;
        mScreenHeight = metrics.heightPixels;
    }

    public boolean isLandscape() {
        updateScreenWidthAndHeight();
        return mScreenWidth > mScreenHeight;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        updateScreenWidthAndHeight();
        if (isLandscape()) {
            mDefaultVideoContainerWidth = mScreenWidth;
            mDefaultVideoContainerHeight = mScreenHeight;
        } else {
            mDefaultVideoContainerWidth = mScreenWidth;
         //   mDefaultVideoContainerHeight = mScreenWidth * 9 / 16;
            mDefaultVideoContainerHeight=mScreenHeight;
        }
        setMeasuredDimension(mDefaultVideoContainerWidth, mDefaultVideoContainerHeight);
    }

    public int getOrientation() {
        return mOrientation;
    }

    public void setOrientation(int orientation) {
        if (getContext() instanceof Activity) {
            Activity mActivity = (Activity) getContext();
            switch (orientation) {
                case ORIENTATION_PORTRAIT:
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    break;
                case ORIENTATION_LANDSCAPE:
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    break;
                case ORIENTATION_SENSOR_LANDSCAPE:
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                    break;
                case ORIENTATION_SENSOR_PORTRAIT:
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                    break;
                case ORIENTATION_SENSOR:
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
                    break;
                case ORIENTATION_LOCKED:
                    mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
                    break;
            }
            mOrientation = orientation;
            invalidate();
        }
    }

    public void locked() {
        mLastOrientation = mOrientation;
        setOrientation(ORIENTATION_LOCKED);
    }

    public boolean isLocked() {
        return mOrientation == ORIENTATION_LOCKED ? true : false;
    }

    public void unlocked() {
        setOrientation(mLastOrientation);
    }

    public void toggleOrientation() {
        if (getContext() instanceof Activity && mOrientation != ORIENTATION_LOCKED) {
            Activity mActivity = (Activity) getContext();
            if (isLandscape()) {
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
            } else {
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            }
            if (mOrientation == ORIENTATION_SENSOR) {
                getHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setOrientation(mOrientation);
                    }
                }, 2000);
            }
        }
    }
}

Second add this to your layout, xml file like this :

    <yourPackage_name.URotateLayout
        android:id="@+id/rotate_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

     <your_layout_here/>

    </yourPackage_name.URotateLayout>

finally use it in your activity or fragment like this :


   //define the view i use the **butterknife**
   @Bind(R.id.rotate_layout)
    URotateLayout rotateLayout;



    @Override
    public void toggleScreenOrientation() {
        if (rotateLayout != null) {
            rotateLayout.toggleOrientation();
        }
    }

    //use this for set oriention
    public void setScreenOriention(int oriention) {
            rotateLayout.setOrientation(oriention);
    }