Screen Orientation

You can use the ScreenOrientation module to control the screen orientation and to set a callback to receive orientation change events.


Before you can make use of the ScreenOrientation API, you need to include the screenorientation extension in your build.yml

extensions: ["screenorientation"]

Get current screen orientation

You can get the current orientation of the device by using the System.screenOrientation property.

Ruby syntax:

screen_orientation = Rho::System.screenOrientation
if screen_orientation == "portrait"
    # do something specific to portrait mode
elsif screen_orientation == "landscape"
    # do something specific to landscape mode

JavaScript syntax:

var screen_orientation = Rho.System.screenOrientation;
if (screen_orientation === "portrait") {
    // do something specific to portrait mode
} else if (screen_orientation === "landscape") { 
    // do something specific to landscape mode

Control screen orientation

Locking the orientation

In some applications, it may be necessary to force the display to always be locked to an orientation. You can do this by setting the disable_screen_rotation in rhoconfig.txt

#disable screen rotation (enabled by default)
disable_screen_rotation = 1 

Enable / disable auto-rotation

You can enable or disable the autorotation of the display when the device is rotated by setting the autoRotate property

Ruby syntax:

# enable auto-rotate
Rho::ScreenOrientation.autoRotate = true

# disable auto-rotate
Rho::ScreenOrientation.autoRotate = false

JavaScript syntax:

// enable auto-rotate
Rho.ScreenOrientation.autoRotate = true;

// disable auto-rotate
Rho.ScreenOrientation.autoRotate = false;

Changing to default orientation

Set the screen to the default orientation of the device using the normal method.

Ruby syntax:


JavaScript syntax:


Changing to right-handed orientation

Set the screen to right-handed orientation using the rightHanded method.

Ruby syntax:


JavaScript syntax:


Changing to left-handed orientation

Set the screen to left-handed orientation using the leftHanded method.

Ruby syntax:


JavaScript syntax:


Changing to upside-down orientation

Set the screen to upside-down orientation using the upsideDown method. This can be useful, for example, when the user offers the device to a customer to obtain a signature. If you set the orientation to upsideDown, the application’s user interface will be in the correct position from the customer’s point of view, without them having to physically rotate the device. This streamlines the capture process, avoids cumbersome manual operations and reduces the risk of the device being dropped.

Ruby syntax:


JavaScript syntax:


Responding to screen orientation change

When the device is rotated, it is sometimes necessary to update your application’s UI layout to better suit the new orientation. There are two different approaches, depending on where the logic for change is placed: The first is to handle UI adaptations yourself (with code) and the second is to use responsive design.

Handling rotation in code

If you want to respond to the device being rotated by running some code, there are two steps that must be completed. The first is to set a callback in order to be notified of orientation change events and the second is to update the UI layout accordingly.

Set the callback for screen orientation events by using the setScreenOrientationEvent method.

Ruby syntax:

def set_callback
    Rho::ScreenOrientation.setScreenOrientationEvent(url_for(:action => :screen_orientation_callback))

def unset_callback

def screen_orientation_callback
    puts "Screen Rotated. New orientation: #{@params["result"]}"

JavaScript syntax:

function set_callback() {

function unset_callback() {

function screen_orientation_callback(params) {
    console.log("Screen Rotated. New orientation: "+params.result);

Updating the UI on screen orientation change

You can make your application handle different screen orientations by creating two different screen layouts for portrait and landscape orientation.

In your ERB files, You can write if ... else code in your layout using the System.screenOrientation property to change the layout of elements.

<% if System.screenOrientation == 'portrait' %>
    <!-- HTML for portrait layout -->
<% else %>
    <!-- HTML for landscape layout -->
<% end %>   

While this method works, there is a better solution: responsive design.

Responsive design

The easiest way to show different UI layouts to the user based on the orientation and even automatically adjust to various device sizes is by using responsive design.

Using responsive design helps avoid creating entirely different versions of the same content for different orientations and sizes. This technique makes use of CSS3 Media queries to allow content to adapt to conditions such as screen size or aspect ratio.

For example, you can make the layout on your UI show content stacked in portrait mode and side-by-side in landscape mode.

<!DOCTYPE html>
<html lang="en">
  <style type="text/css">
    .block {
      float: left;
      height: 200px;
      width: 100%;
    @media screen and (min-aspect-ratio: 1/1) {
      /* these styles will only be applied when the viewing area is square or landscape */
      .block {
        width: 50%;
    .box {
      border: 1px solid red;
      width: 100%;
      height: 100%;
  <div class="block"><div class="box">Block 1</div></div>
  <div class="block"><div class="box">Block 2</div></div>
  <div class="block"><div class="box">Block 3</div></div>

Here is how the layout looks in portrait…


…and here is how it adapts to landscape


Responsive design frameworks

There are some CSS frameworks available that are designed to make it easier to use responsive design in your applications. Two of the most popular are:

Both are modern frameworks, designed to take advantage of the latest capabilities of today’s mobile browsers. They provide a number of features to shorten your development time while simultaneously making your code more maintainable and future-proof. Particularly if you are developing for more than one screen size, using a responsive framework is highly recommended.

Related reading

Back to Top