Using the PROCESS_TEXT Intent Action in Android

Ismail Nurudeen
4 min readMay 26, 2020

--

Shortly after publishing my ClipNote app on Google PlayStore I started getting a lot of questions from Android developers about how the app works under the hood,

The ClipNote app allows you to select text anywhere,copy and save them directly as notes without having to open the app.

When you select a text, it shows an option to Copy with ClipNote along with other default text actions like copy,paste and cut.

The concept behind the app is very simple and in this article,I’m going to talk about how the Copy with ClipNote option was added to the text actions tray.

So let’s get started…

At the core of the app is an Intent action, precisely the ACTION_PROCESS_TEXT .

As an android developer you probably have used intents in various scenarios, maybe just for starting a new activity or sending an SMS with the SMS app.

The PROCESS_TEXT Intent which was added since Android 6.0 (Marshmellow — SDK 24) is no different from other intents, it allows you perform an action when text is selected and your app listen for those actions.

This feature is being used by some popular apps such as Google Translate and Gmail.

Now let’s see how to use this intent in our apps and listen for text selections in other apps.

We would build a very simple app that receives text via the text processing intent and counts the number of text that was selected.

There are 3 basic steps.

  • Create Project
  • Add Intent Filter to Manifest
  • Handle Intent

— Create Project

Create new project and call it what ever you want, just make sure you set minimum SDK to 23.

Create an activity called CountTextActivity, we would use this activity to handle our intent in the future.

Add the following code to the activity’s corresponding layout file activity_count_text.xml

activity_count_text.xml

This is to display the text count in an activity.

— Add Intent Filter to Manifest

Just as usual,if you want your app to listen to certain intents from other apps you have to specify the intent-filter and the activity that would handle the intent.

So go to your manifest file and add the CountTextActivity with the following intent filter.

AndroidManifest.xml

The label property would be the text that would display on the action tray, it usually should be a verb (an action word), The <action/> tag specify the intent we are listening for and the mimeType property of the <data/> tag specifies the type of data we are going to receive and handle with the intent, in our case plain text.

— Handle Intent

We are going to handle the incoming intent inside the CountTextActivity as the activity would be automatically opened when the PROCESS_TEXT intent action of the app is triggered.

Inside the CountTextActivity, we are going to check for the intent and if the intent action matches the PROCESS_TEXT intent action then we will get the data from the intent which would be the selected text and count it.

CountTextActivity.kt with Layout File.

And with the above, you get an app that allows you to count text selected from other apps.

Counting text by opening activity.

The only issue here is that we might not want to display the text in an activity, we really don’t want to open a new activity just to count some text.

So what we are going to do is display a toast instead, without showing the CountTextActivity.

Note: We are still going to open the activity as we can’t handle the intent without an activity,but this time the activity will be invisible and dismissed almost immediately.

Counting text without opening activity (What we want).

We are going to make our activity invisible by setting the theme to Theme.NoDisplay.

Your CountTextActivity in manifest should now look like this;

New AndroidManifest.xml

and your CountTextActivity.kt file should now look like this;

New CountTextActivity.kt

Now we have an app that can perform an action on the selected text without displaying an activity.

If the selected text is in an editable view,you can also send text back to the view like replace the text, add some editing etc…

See Code on Github with a little modification to make it a WordCounter.

https://github.com/ismailnurudeen/WordCounter

For further reading check out this article by Ian LakeCreating custom Text Selection actions with ACTION_PROCESS_TEXT

You can checkout the ClipNote app on PlayStore to see ACTION_PROCESS_TEXT in action.

https://play.google.com/store/apps/details?id=xyz.ismailnurudeen.clipnote

I sincerely do hope you found this material helpful and if you did, don’t forget to clap 👏 and share this article with fellow Android Developers.

If you encountered any issues while following this article or you have a suggestion to make, please leave a comment below and I’d be pleased to reply you.

Don’t forget to follow me on twitter @TeenMutantCoder 🐦 and like my page on Facebook too… Thanks for your time 🙌

Did you know? You can clap multiple times. 👏

--

--

Ismail Nurudeen
Ismail Nurudeen

Written by Ismail Nurudeen

Smart Apps Developer | Android | Kotlin | Machine Learning | Technical Lead @DeepSyntax Ex. Android Dev @hotelsng