3

I want to make a widget using android and i need to catch any text selection event. For example a user is reads sth. and selects text in another application (web browser, pdf reader,messaging).

Is that possible to catch any text selection in background?

afrikaan
  • 425
  • 5
  • 21
  • Using clipboard is best way, here's an [Example](http://www.tutorialspoint.com/android/android_clipboard.htm) – Apurva Mar 03 '15 at 06:57
  • 1
    @kyur did you find any w ay to detect text selection in andorid in any other application?I mean text selection not text copy. – Mahendra Chhimwal Jul 19 '16 at 12:07
  • I want this too. Not copy, only selection. Is this possible? – Sergey Zaitsev Feb 06 '17 at 16:11
  • @Mr.Newman MahendraChhimwal did u find any method – Mrad4Tech May 19 '17 at 08:06
  • With some articles I mean that you can do this only in your own application (with some modification of context menu), but in another application you cannot do this in order to security (one app against another). So only when text is copied (to clipboard) all apps can read it. But not a selection – Sergey Zaitsev May 20 '17 at 07:24

3 Answers3

1

try this to get text from clipboard

     ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
     clipboard.getText();

Set text and get text from clipboard

Community
  • 1
  • 1
Nooh
  • 1,548
  • 13
  • 21
1

enter image description here

Sample code:

String textToPaste = null;

ClipboardManager clipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);

/* Returns true if there is currently a primary clip on the clipboard. */

if (clipboard.hasPrimaryClip()) {
    ClipData clip = clipboard.getPrimaryClip();

    // if you need text data only, then you have to check the MIME type for Text as i shown below :
    if (clip.getDescription().hasMimeType(MIMETYPE_TEXT_PLAIN))
        // WARNING: The item could cantain URI that points to the text data.
        // In this case the getText() returns null and this code fails!
        textToPaste = clip.getItemAt(0).getText().toString();

    // or you may coerce the data to the text representation: i have explained this in the second image.
    textToPaste = clip.getItemAt(0).coerceToText(this).toString();
}

if (!TextUtils.isEmpty(textToPaste))
     ((TextView)findViewById(R.id.etInput1)).setText(textToPaste);

For more info , Kindly have a look on the offical link

http://developer.android.com/reference/android/content/ClipData.html

http://developer.android.com/reference/android/content/ClipboardManager.html

enter image description here

Sankar Ganesh PMP
  • 11,927
  • 11
  • 57
  • 90
1

1.you can define a service to listen clipboard event in background

 ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
    clipboardManager.setPrimaryClip(new ClipboardManager.OnPrimaryClipChangedListener(){
        @Override
        public void onPrimaryClipChanged() {
            //TODO do your work

        }
    });

2.or you can use AccessibilityService to detect copy text event, listen "TYPE_VIEW_TEXT_SELECTION_CHANGED" event and read data from clipboard, but you should guide user to enable accessibility switch for your app in System's Settings.

Jishi Chen
  • 634
  • 7
  • 17