Text selection events and notifications
Create custom listeners for TextSelectionManager to handle selection changes and entry into or exit from text selection mode:
class MyActivity : PdfActivity(), TextSelectionManager.OnTextSelectionChangeListener, TextSelectionManager.OnTextSelectionModeChangeListener {
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requirePdfFragment().addOnTextSelectionModeChangeListener(this) requirePdfFragment().addOnTextSelectionChangeListener(this) }
override fun onDestroy() { pdfFragment?.removeOnTextSelectionModeChangeListener(this) pdfFragment?.removeOnTextSelectionChangeListener(this) super.onDestroy() }
override fun onEnterTextSelectionMode(controller: TextSelectionController) { Log.i(TAG, "Text selection mode has started.") }
override fun onExitTextSelectionMode(controller: TextSelectionController) { Log.i(TAG, "Text selection mode has ended.") }
override fun onBeforeTextSelectionChange(newTextSelection: TextSelection?, currentTextSelection: TextSelection?): Boolean { // Return `false` to prevent changes to the current selection state, if needed. return true }
override fun onAfterTextSelectionChange(newTextSelection: TextSelection?, oldTextSelection: TextSelection?) { if (newTextSelection != null) { Log.i(TAG, "Selected text was: ${newTextSelection.text}") } else { Log.i(TAG, "Text selection is cleared.") } }
companion object { private const val TAG = "MyActivity.TextSelection" }}public class MyActivity extends PdfActivity implements TextSelectionManager.OnTextSelectionChangeListener, TextSelectionManager.OnTextSelectionModeChangeListener{
private static final String TAG = "MyActivity.TextSelection";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requirePdfFragment().addOnTextSelectionModeChangeListener(this); requirePdfFragment().addOnTextSelectionChangeListener(this); }
@Override protected void onDestroy() { getPdfFragment().removeOnTextSelectionModeChangeListener(this); getPdfFragment().removeOnTextSelectionChangeListener(this); super.onDestroy(); }
@Override public void onEnterTextSelectionMode(@NonNull TextSelectionController controller) { Log.i(TAG, "Text selection mode has started."); }
@Override public void onExitTextSelectionMode(@NonNull TextSelectionController controller) { Log.i(TAG, "Text selection mode has ended."); }
@Override public boolean onBeforeTextSelectionChange(@Nullable TextSelection newTextSelection, @Nullable TextSelection currentTextSelection) { // Return `false` to prevent changes to the current selection state, if needed. return true; }
@Override public void onAfterTextSelectionChange(@Nullable TextSelection newTextSelection, @Nullable TextSelection oldTextSelection) { if (newTextSelection != null) { Log.i(TAG, String.format("Selected text was: %s", newTextSelection.text)); } else { Log.i(TAG, "Text selection is cleared."); } }}Implement these listeners to respond to text selection events.
These listeners don’t override the default behavior.
To customize toolbar actions, register OnPreparePopupToolbarListener with setOnPreparePopupToolbarListener() on PdfFragment. Edit the toolbar’s menuItems in onPrepareTextSelectionPopupToolbar().
To run actions from your own UI, use the TextSelectionController that onEnterTextSelectionMode() receives.
For instructions on controlling text selection mode and customizing its UI, refer to the text selection guide.