How to dynamically change page layout when changing screen orientation?

Q: How to dynamically change page layout when changing screen orientation?

If you want to display pages in double page mode in landscape and single page mode in portrait, you can do so with the following snippet:

Copy
DynamicPageLayoutActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class DynamicPageLayoutActivity : PdfActivity() {

    override fun onCreate(savedInstanceState: Bundle) {
        val isLandscape = resources.configuration.orientation == ORIENTATION_LANDSCAPE
        val pageLayoutMode = if (isLandscape) PageLayoutMode.DOUBLE else PageLayoutMode.SINGLE
        val newConfiguration = PdfActivityConfiguration.Builder(configuration)
            .layoutMode(pageLayoutMode)
            .build()

        configuration = newConfiguration

        // Make sure to call super on parent PdfActivity.
        super.onCreate(savedInstanceState)
    }
}
Copy
DynamicPageLayoutActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class DynamicPageLayoutActivity extends PdfActivity {

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        boolean isLandscape = getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE;
        PageLayoutMode pageLayoutMode = isLandscape ? PageLayoutMode.DOUBLE : PageLayoutMode.SINGLE;
        PdfActivityConfigurations newConfiguration = new PdfActivityConfiguration.Builder(configuration)
            .layoutMode(pageLayoutMode)
            .build()

        // Set the new configuration.
        setConfiguration(newConfiguration);

        // Make sure to call super on parent PdfActivity.
        super.onCreate(savedInstanceState);
    }
}

Note that PageLayoutMode#AUTO setting leads to similar behavior where double page mode is enabled in landscape mode but only for devices with smaller dimension larger than 540dp (e.g. tablets).