How to Dynamically Change the Page Layout When Changing the Screen Orientation
If you want to display pages in double-page mode in landscape mode and single-page mode in portrait mode, you can do so with the following snippet:
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 the parent `PdfActivity`. super.onCreate(savedInstanceState) } }
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 the parent `PdfActivity`. super.onCreate(savedInstanceState); } }
Note that the PageLayoutMode#AUTO
setting leads to similar behavior, where double-page mode is enabled in landscape mode, but only for devices with a screen width larger than 540dp (e.g. tablets).