SlidingPaneLayout dual-screen and foldable features

SlidingPaneLayout is a control that uses Jetpack Window Manager to adapt to dual-screen and foldable devices. It can show two panes side-by-side (if there's room), otherwise only the first pane will be shown and the second can be revealed by the user (sliding from the side) or programmatically. It adapts to foldable and dual-screen devices by aligning the panes on either side of the hinge or fold.

Single-screen devices, including tablets, will show both panes if the width of the content in each fits side-by-side on screen. If the either pane's contents are too wide, the first pane is shown full screen and the second pane can be shown programmatically or by sliding.

Four Android tablets showing the SlidingPaneLayout sample in different states: split screen landscape, split screen portrait, full screen left pane, and full screen right pane

On the Surface Duo, on a single screen the control behaves like any other device, where the panes will be side-by-side if they fit, otherwise shown independently. When the app is spanned, SlidingPaneLayout will automatically put each pane on a screen, on either side of the hinge. In dual-landscape mode, SlidingPaneLayout does not alter the layout and it behaves like a single tall screen - in this scenario the individual panes should scroll or otherwise ensure their content isn't hidden.

Seven Surface Duo devices showing the SlidingPaneLayout sample in different states

Add dependencies

To use SlidingPaneLayout in your project, update the build.gradle file.

  1. Ensure the project is compiling with API level 31:

    compileSdk 31
  2. Add these dependencies so we can access the APIs needed:

    dependencies {
       implementation "androidx.slidingpanelayout:slidingpanelayout:1.2.0"

SlidingPaneLayout in your user interface

This XML snippet can be used to add the SlidingPaneLayout to a view - it should be the root (or top-level) element on the screen:

      <!-- first child -->
      <!-- second child -->

The XML below (from this sample) shows a LinearLayout being used for the first pane and a FragmentContainerView for the second pane:

<androidx.slidingpanelayout.widget.SlidingPaneLayout xmlns:android=""
            android:text="Side Pane"
            android:textStyle="bold" />
        <!-- other elements removed for clarity-->
        android:background="@android:color/holo_red_dark" />

This screenshot shows how the above XML works on the Surface Duo when the app is spanned:

SlidingPaneLayout spanned across both screens of a Surface Duo

On a single-screen device (whether its a phone or a tablet) the two panes are laid out according to the size rules for SlidingPaneLayout: if they can both fit in the view according to their required widths, they'll show side-by-side. If they cannot fit in the view, the first pane is shown and the second pane can be programmatically shown or dragged into view (unless programmatically blocked).


Code on this page comes from this SlidingPaneLayout demo app.

The eBook Reader, TwoDo, and TravelPlanner apps on the samples page all use SlidingPaneLayout to create real-world user interface examples.

Google also updated their Trackr sample with SlidingPaneLayout to provide an adaptive single-screen, dual-screen, foldable, and large screen user experience.