Changing Android Device Orientation with ADB

Changing Android Device Orientation with ADB

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Setting the orientation of an Android device programmatically can be crucial for testing and automation purposes. If you want to set the device orientation via ADB (Android Debug Bridge) without relying on UI Automator, there are several methods you can utilize. This guide will walk you through the most effective solutions.

Disabling Auto-Rotation

Before setting a specific orientation, you need to disable the automatic rotation feature on your device. This ensures that the device maintains the orientation you set, regardless of physical movement.

adb shell settings put system accelerometer_rotation 0

The accelerometer_rotation setting controls the auto-rotation feature:

  • 0 – Disable auto-rotation
  • 1 – Enable auto-rotation

Setting the User Rotation

Once auto-rotation is disabled, you can set the device orientation using the user_rotation setting. This setting accepts values that correspond to different orientations:

  • 0 – Portrait
  • 1 – Landscape (90° clockwise)
  • 2 – Portrait Reversed (180°)
  • 3 – Landscape Reversed (270° clockwise)

For example, to set the device to landscape reversed (270° clockwise), you would use the following commands:

adb shell settings put system user_rotation 3

Alternative Method Using Content Provider

An older method involves using the content provider to insert values directly into the system settings. This method is less clean but still effective:

adb shell content insert --uri content://settings/system --bind name:s:accelerometer_rotation --bind value:i:0
adb shell content insert --uri content://settings/system --bind name:s:user_rotation --bind value:i:1

However, the preferred method is using the settings command as described above, as it is more straightforward and reliable.

Using wm Command

In newer versions of Android, you can also use the wm command to set user rotation. This method is particularly useful for advanced configurations:

adb shell wm user-rotation lock 0

The wm command allows you to lock the device orientation, providing a more flexible approach to managing device settings.

Automating Orientation Changes with Repeato

For developers and testers looking to automate these processes, using a no-code test automation tool like Repeato can be highly beneficial. Repeato allows you to create, run, and maintain automated tests for iOS and Android apps quickly and efficiently.

With Repeato, you can execute ADB commands as part of your test scripts, allowing you to automate orientation changes seamlessly. This tool leverages computer vision and AI, making it faster and more intuitive to edit and run tests.

For more details on how Repeato can enhance your testing workflow, visit our documentation or contact us for more information.

Like this article? there’s more where that came from!