This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /guides/android/events-and-notifications/text-selection.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. Android PDF text selection events and notifications | Nutrient

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"
}
}

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.