Android Tutorial Creating custom Text Selection actions with ACTION_PROCESS_TEXT

Android 6.0 Marshmallow introduced a new floating text selection toolbar, which brings the standard text selection actions, like cut, copy, and pastes, closer to the text you’ve selected. Even better though is the new ACTION_PROCESS_TEXT which makes it possible for any app to add custom actions to that text selection toolbar.


1*D4zZzPlBTk5cEN9Qn0-cBA.gif

The text selection toolbar in Android 6.0
Apps like Wikipedia and Google Translate are already taking advantage of it to instantly lookup or translate the selected text.

But finding information on implementing ACTION_PROCESS_TEXT and adding your own actions? That’s what this thread will cover.


Step 1: Set up Intent Filter
Code: Copy
AddActivityText(main,
<intent-filter>
    <action android:name="android.intent.action.PROCESS_TEXT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>)

Step 2: Handle Intent in your Activity
Code: Copy
    Try
        If Activity.GetStartingIntent.Action="android.intent.action.PROCESS_TEXT" Then
            Dim strAction as String=Activity.GetStartingIntent.GetExtra("android.intent.extra.PROCESS_TEXT")
            Msgbox(strAction, "Text selected:")
        End If
    Catch
        Log(LastException)
    End Try



In the above code, the Label Text would be same as the application Label. To customize this, we can add a new activity, and specify a new label:
Code: Copy
'For Creating custom Text Selection actions with ACTION_PROCESS_TEXT
SetActivityAttribute(ACTION_PROCESS_TEXT, android:label, "ACTION_PROCESS_TEXT DEMO")
AddActivityText(ACTION_PROCESS_TEXT,
<intent-filter>
    <action android:name="android.intent.action.PROCESS_TEXT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>)

Inside Activity_Create of ACTION_PROCESS_TEXT:
Code: Copy
    Try
        If Activity.GetStartingIntent.Action="android.intent.action.PROCESS_TEXT" Then
            Main.strActoin=Activity.GetStartingIntent.GetExtra("android.intent.extra.PROCESS_TEXT")
            StartActivity(Main)
        End If
    Catch
        Log(LastException)
    End Try
    Activity.Finish


Screenshot_20190115-110010_Chrome.jpgScreenshot_20190115-110022.jpg
 

Attachments

Back
Top