Book Excerpt: Floating Action Mode and ACTION_PROCESS_TEXT

The following is an excerpt from Version 6.8 of “The Busy Coder’s Guide to Android Development”:

On Android 6.0, if you highlight text, you will see a new floating action mode, where cut, copy, and paste operations reside:

Floating Action Mode

If you tap that overflow indicator on the action mode, a fly-out menu will appear… one that contains arbitrary apps, in addition to system-supplied options:

Floating Action Mode, Showing Overflow with Custom Apps

In this case, the MNC “API Demos” app appears as an option. Choosing it pops up an activity that has access to the highlighted text from the preceding activity:

API Demos Application, Showing Text Selection

Replacing the value in the field and clicking the button puts your replacement text in as a replacement for whatever you had highlighted.

The “API Demos” activity in question, that is receiving the text, supports ACTION_PROCESS_TEXT as the Intent action:

<intent-filter >
    <action android:name="android.intent.action.PROCESS_TEXT"/>
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

Note that this <intent-filter> also limits the MIME type to be text/plain, though it is unclear if there are any other valid options for that MIME type.

EXTRA_PROCESS_TEXT will hold some text, or EXTRA_PROCESS_TEXT_READONLY will hold it if the text is read-only. The activity will be invoked via startActivityForResult(). The result Intent can have its own EXTRA_PROCESS_TEXT value, which will be the replacement text.