Android - Allow rotate into portrait mode but do not rotate external display

Try these instructions, using a computer with adb installed and your phone connected:

Android Jelly Bean (4.2) locks HDMI rotation by default. You can unlock by the instruction of this commit.

  1. Add special mirroring modes for demonstration purposes.

  2. Assume rotation of HDMI display is portrait.

    adb shell setprop persist.demo.hdmirotation portrait
    
  3. Don't lock rotation while HDMI is plugged in.

    adb shell setprop persist.demo.hdmirotationlock false
    
  4. Hide secondary displays from apps but continue mirroring to them.

    adb shell setprop persist.demo.singledisplay true
    

Source: https://community.freescale.com/docs/DOC-97740


I know this is an old post but I was just dealing with a similar issue but trying to cast the screen of a Pixel 2 device to a portrait screen and was running into a lot of issues. The main problem seems to be that Pixel 2 and 3 do not seem to support HDMI output so these properties do not solve the issue.

However, I was able to figure this out by following the link Malvineous posted in a comment above. Just below the code dealing with setting persist.demo.hdmirotation there is another setting called persist.demo.remoterotation as well as one called persist.demo.rotationlock. These were the key to getting a phone to cast in portrait mode to the screen. These settings seem to exist in both 8.0 and 9.0 versions of Android, though I downgraded my Pixel 2 to 8.0 in the process of trying to figure this out and have not tested on 9.0 yet.

To get to this point took several steps which are outlined below.

  1. You need to unlock and root your phone (I used MagiskManager to help root and I found this guide very useful as it explains a little more in detail exactly what to do for those like me who had not done this before).
  2. Using adb tool start the adb shell on your connected and rooted device adb shell
  3. Activate superuser by entering su in shell (for me this had the additional step of going into MagiskManager and allowing SuperUser access to the shell).
  4. Enter the following lines into the shell to create and set these properites:
    setprop persist.demo.remoterotation portrait
    setprop persist.demo.rotationlock false
    

Once that was set, I cast my Pixel 2's screen to a chromecast device and it was in the desired portrait orientation. I hope this helps anyone in the future trying to figure this out.