Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the new Android M feature of "Text Selection" to be offered from outside of your app?

Background

Android M presents a new way to handle selected text (link here), even from outside of your app . Text selection can be handled as such:

enter image description here

I know it's possible to handle the selected text from outside the app, because if I go to the web browser (or any other place that allows text selection), I can see that I can use the "API demos" app to handle the selected text.

The problem

I can't see a lot of information about how to do it.

The question

  1. What should be added in code (and manifest) to be able to handle the selected text from outside the app ?
  2. Is it possible to limit the selection to certain types of texts ? For example, offer to show the app only if the text type is a valid phone number ?
like image 282
android developer Avatar asked May 28 '15 22:05

android developer


1 Answers

First, to clarify the question: On an M emulator, if you highlight text, you will see the new floating action mode. If you click the overflow icon, you will see "API DEMOS" show up:

M Developer Preview Emulator

Clicking that brings up an activity from the API Demos app, showing the highlighted text:

Another M Developer Preview Emulator

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


WARNING: The following explanation is from inspecting the API Demos code and the M Developer Preview documentation. It is very possible that this will change before M ships for realz. YMMV, unless you use the metric system, in which case YKMV.

The activity in question, that is receiving the text, supports ACTION_PROCESS_TEXT as the Intent action. 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.

So, to the specific questions:

What should be added in code (and manifest) to be able to handle the selected text from outside the app ?

See above. Note that the API Demos activity (ProcessText) has this <intent-filter>:

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

The documentation does not discuss a MIME type. I have not run any experiments to determine if the MIME type is required, and what else we might get (text/html for stuff that has spans?).

Is it possible to limit the selection to certain types of texts ? For example, offer to show the app only if the text type is a valid phone number ?

That wouldn't seem to be possible given the documentation. That being said, it's certainly a reasonable idea (e.g., advertise a regex, or multiple regexes, via metadata in the manifest that the text must match).

like image 157
CommonsWare Avatar answered Nov 12 '22 08:11

CommonsWare