Changing configuration at runtime
You might want to change the configuration used to display your documents at runtime. You can do this with either PdfActivity or PdfFragment. Continue reading for instructions on how to change the configuration for each of these.
Changing the configuration of the PdfActivity
To change the configuration of the PdfActivity, call PdfActivity#setConfiguration, which saves the state and recreates the activity with the new configuration:
class DynamicConfigurationActivity : PdfActivity() {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
// Add a toolbar item that toggles the scroll direction. setToolbarItemsProvider { defaultItems -> defaultItems + ToolbarItem.Button( actionId = "my.app.toggle_scroll_direction", id = R.id.toggle_scroll_direction, contentDescription = "Toggle scroll direction", icon = ToolbarIcon.Resource(R.drawable.ic_swap), ) } setOnToolbarItemClickListener { id -> if (id != R.id.toggle_scroll_direction) return@setOnToolbarItemClickListener false // Create the configuration builder prefilled from the current configuration. val newConfiguration = PdfActivityConfiguration.Builder(configuration) .scrollDirection( if (configuration.configuration.scrollDirection == PageScrollDirection.HORIZONTAL) PageScrollDirection.VERTICAL else PageScrollDirection.HORIZONTAL) .build() // Set the configuration on the activity. This recreates the activity, similar to changing the orientation or language. configuration = newConfiguration true } }}public class DynamicConfigurationActivity extends PdfActivity {
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
// Add a toolbar item that toggles the scroll direction. setToolbarItemsProvider(defaultItems -> { final List<ToolbarItem> items = new ArrayList<>(defaultItems); items.add(new ToolbarItem.Button( "my.app.toggle_scroll_direction", R.id.toggle_scroll_direction, "Toggle scroll direction", new ToolbarIcon.Resource(R.drawable.ic_swap), new ToolbarItemState(true, false, true), ToolbarItem.ItemEdge.End, ToolbarItem.OverflowBehavior.IfRoom)); return items; }); setOnToolbarItemClickListener(id -> { if (id != R.id.toggle_scroll_direction) return false; final PdfActivityConfiguration configuration = getConfiguration(); // Create the configuration builder prefilled from the current configuration. final PdfActivityConfiguration newConfiguration = new PdfActivityConfiguration.Builder(configuration) .scrollDirection( configuration.getConfiguration().getScrollDirection() == PageScrollDirection.HORIZONTAL ? PageScrollDirection.VERTICAL : PageScrollDirection.HORIZONTAL) .build(); // Set the configuration on the activity. This recreates the activity, similar to changing the orientation or language. setConfiguration(newConfiguration); return true; }); }}If you want to change the configuration immediately after the activity is opened, we recommend doing it before onCreate(). In this case, we won’t restart the current activity, thereby making the configuration change much quicker:
class StartupConfigurationChangeActivity : PdfActivity() {
override fun onCreate(savedInstanceState: Bundle) { val newConfiguration = PdfActivityConfiguration.Builder(configuration) // Change your configuration. ... .build()
// Set the new configuration. this.configuration = newConfiguration
// Make sure to call `super.onCreate()` on the parent `PdfActivity`. super.onCreate(savedInstanceState) }}public class StartupConfigurationChangeActivity extends PdfActivity {
@Override protected void onCreate(Bundle savedInstanceState) { PdfActivityConfiguration newConfiguration = PdfActivityConfiguration.Builder(configuration) // Change your configuration. ... .build();
// Set the new configuration. setConfiguration(newConfiguration);
// Make sure to call `super.onCreate()` on the parent `PdfActivity`. super.onCreate(savedInstanceState); }}Changing the configuration of the PdfFragment
To change the configuration of the PdfFragment, you need to:
- Save the state of your current fragment using
PdfFragment#getState. - Create a new fragment with the desired configuration using
PdfFragment#newInstance. - Replace the original fragment with the new one.
- Restore the state using
PdfFragment#setState.
/** The currently displayed `PdfFragment`. **/private var fragment: PdfFragment? = null set(fragment) { // Remove the previous fragment. this.fragment?.let { it.removeDocumentListener(this) } field = fragment if (fragment != null) { // Replace the old fragment with a new one. supportFragmentManager .beginTransaction() .replace(R.id.fragmentContainer, fragment) .commit() // Register the activity to be notified when the document is loaded. fragment.addDocumentListener(this) } }
/** Switches between horizontal and vertical scrolling. */private fun toggleScrollDirection() { val currentFragment = fragment val currentDocument = fragment?.document if (currentFragment == null || currentDocument == null || currentFragment.configuration == null) return
val scrollDirection = currentFragment.configuration.scrollDirection
// Copy the existing configuration to a new configuration builder. val configurationBuilder = PdfConfiguration.Builder(currentFragment.configuration)
// Toggle the scroll direction. configurationBuilder.scrollDirection( if (scrollDirection == PageScrollDirection.HORIZONTAL) PageScrollDirection.VERTICAL else PageScrollDirection.HORIZONTAL)
// Save the old fragment state. val state = currentFragment.state // Create the fragment with the already loaded document and the new configuration. val newFragment = PdfFragment.newInstance(currentDocument, configurationBuilder.build()) fragment = newFragment // Restore the fragment state. newFragment.state = state}/** The currently displayed `PdfFragment`. **/private PdfFragment fragment;
/** Replaces the currently displayed fragment. */private void setFragment(@NonNull PdfFragment fragment) { // Remove the previous fragment. if (this.fragment != null) { this.fragment.removeDocumentListener(this); } this.fragment = fragment;
// Replace the old fragment with a new one. getSupportFragmentManager() .beginTransaction() .replace(R.id.fragmentContainer, fragment) .commit();
// Register the activity to be notified when the document is loaded. fragment.addDocumentListener(this);}
/** Switches between horizontal and vertical scrolling. */private void toggleScrollDirection() { if (fragment == null || fragment.getDocument() == null || fragment.getConfiguration() == null) return;
PageScrollDirection scrollDirection = fragment.getConfiguration().getScrollDirection();
// Copy the existing configuration to a new configuration builder. PdfConfiguration.Builder configurationBuilder = new PdfConfiguration.Builder(fragment.getConfiguration());
// Toggle the scroll direction. configurationBuilder.scrollDirection( scrollDirection == PageScrollDirection.HORIZONTAL ? PageScrollDirection.VERTICAL : PageScrollDirection.HORIZONTAL);
// Save the old fragment state. Bundle state = fragment.getState(); // Create the fragment with the already loaded document and the new configuration. PdfFragment newFragment = PdfFragment.newInstance(fragment.getDocument(), configurationBuilder.build()); setFragment(newFragment); // Restore the fragment state. newFragment.setState(state);}For more details, you can check out RuntimeConfigurationExample and CustomFragmentRuntimeConfigurationExample in the Catalog app.