12

When the user marks some text (inside of an EditText, WebView...) a floating text selection popup appears, where apps can add custom items. Can someone please give me an example, how to add an item to this popup menu which makes an intent and transfers the selected String to my activity.

enter image description here

pnuts
  • 58,317
  • 11
  • 87
  • 139

1 Answers1

17

This blog tutorial will show you how: https://medium.com/google-developers/custom-text-selection-actions-with-action-process-text-191f792d2999

Basically, in your Manifest file, add PROCESS_TEXT intent filter to the activity that will handle the text shared from popup menu.

<activity
    android:name=".ProcessTextActivity"
    android:label="@string/process_text_action_name">
  <intent-filter>
    <action android:name="android.intent.action.PROCESS_TEXT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
  </intent-filter>
</activity>

Then, you would process that text in your Activity like this

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.process_text_main);
  CharSequence text = getIntent()
      .getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
  // process the text
}
AnT
  • 801
  • 9
  • 9
  • Is the context menu of text selection something of the OS itself, or of something else? Can it be modified by the app that hold the EditText? – android developer Jul 19 '19 at 23:05
  • The way u did is working great but I am using navigation component with fragments, now i only have one activity. I need to add multiple option in floating text selection menu. How to approach this as i don't want to create empty activities ? – xaif Jul 21 '21 at 23:56