Android how to get string from clipboard onPrimaryClipChanged?

redrom

I'm trying to get text copied into the clipboard using the following listener:

import android.content.ClipboardManager.OnPrimaryClipChangedListener;
import com.orhanobut.logger.Logger;

public class ClipboardListener implements OnPrimaryClipChangedListener
{

    public void onPrimaryClipChanged()
    {
        // do something useful here with the clipboard
        // use getText() method
        Logger.d("Clipped");
    }
}

The listener is initialized as follows:

ClipboardManager clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipBoard.addPrimaryClipChangedListener( new ClipboardListener());

After the text is copied into the clipboard onPrimaryClipChanged is fired, but I don't know how to get the copied text in this method using ClipboardManager.getPrimaryClip() because the method is not available from the context and is not passed in the param of onPrimaryClipChanged.

Karen Forde

I would suggest adding the listener as follows instead of creating a new class. I have included how to get text from the ClipData.

You mention being unable to access your context in the listener, I've added a comment within the code below showing how to do so.

ClipboardManager clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
clipBoard.addPrimaryClipChangedListener(new OnPrimaryClipChangedListener() {

    @Override
    public void onPrimaryClipChanged() {
        ClipData clipData = clipBoard.getPrimaryClip();
        ClipData.Item item = clipData.getItemAt(0);
        String text = item.getText().toString();

        // Access your context here using YourActivityName.this
    }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android how to get string from clipboard onPrimaryClipChanged?

From Dev

Android - Get text from clipboard whenever user copy to clipboard

From Dev

C++ Get string from Clipboard on Linux

From Dev

How to copy text to Clipboard from TextField in Android

From Dev

Android how to get a String from the TextView to a String

From Dev

Android how to get a String from the TextView to a String

From Dev

Concat a string to clipboard in android

From Dev

Read string from clipboard

From Dev

How to get formatted Date from string android?

From Dev

How to get value from a string in Android

From Dev

How to get string from PHP server to Android?

From Dev

How to get time in milliseconds from a string in android?

From Dev

How to copy string to clipboard

From Dev

Android clipboard.getText() is deprecated; how then to get the text item that's in it?

From Dev

How to remove text formatting when pasting from clipboard on Android

From Dev

Retrieving string from clipboard in Java

From Dev

How to ask android to get a string from a specified file?

From Java

How to get string value from Onclick() to onSaveInstanceState() in Android Studio

From Dev

How to get raw string of an image from Bitmap in android?

From Dev

How to get data from a xml file into multiple string arrays in android?

From Dev

Android how to get response string from Callback using OkHttp?

From Dev

How to get only string from HTTPResponse RestApi Android

From Dev

How to get data from a xml file into multiple string arrays in android?

From Dev

How to paste into nano from clipboard?

From Dev

Android get PrivateKey from String

From Dev

Android get PrivateKey from String

From Dev

get data from clipboard (copied from Powerpoint)

From Dev

Remove spaces from a string of text in clipboard

From Dev

Copying a randomly generated string from python to the clipboard

Related Related

  1. 1

    Android how to get string from clipboard onPrimaryClipChanged?

  2. 2

    Android - Get text from clipboard whenever user copy to clipboard

  3. 3

    C++ Get string from Clipboard on Linux

  4. 4

    How to copy text to Clipboard from TextField in Android

  5. 5

    Android how to get a String from the TextView to a String

  6. 6

    Android how to get a String from the TextView to a String

  7. 7

    Concat a string to clipboard in android

  8. 8

    Read string from clipboard

  9. 9

    How to get formatted Date from string android?

  10. 10

    How to get value from a string in Android

  11. 11

    How to get string from PHP server to Android?

  12. 12

    How to get time in milliseconds from a string in android?

  13. 13

    How to copy string to clipboard

  14. 14

    Android clipboard.getText() is deprecated; how then to get the text item that's in it?

  15. 15

    How to remove text formatting when pasting from clipboard on Android

  16. 16

    Retrieving string from clipboard in Java

  17. 17

    How to ask android to get a string from a specified file?

  18. 18

    How to get string value from Onclick() to onSaveInstanceState() in Android Studio

  19. 19

    How to get raw string of an image from Bitmap in android?

  20. 20

    How to get data from a xml file into multiple string arrays in android?

  21. 21

    Android how to get response string from Callback using OkHttp?

  22. 22

    How to get only string from HTTPResponse RestApi Android

  23. 23

    How to get data from a xml file into multiple string arrays in android?

  24. 24

    How to paste into nano from clipboard?

  25. 25

    Android get PrivateKey from String

  26. 26

    Android get PrivateKey from String

  27. 27

    get data from clipboard (copied from Powerpoint)

  28. 28

    Remove spaces from a string of text in clipboard

  29. 29

    Copying a randomly generated string from python to the clipboard

HotTag

Archive