Android Clipboard Read readFromClipboard(Context context)

Here you can find the source of readFromClipboard(Context context)

Description

Get text from the clipboard.

Parameter

Parameter Description
context the application Context

Return

Returns the clipboard text if the clipboard contains text.

Declaration

public static String readFromClipboard(Context context) 

Method Source Code

//package com.java2s;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;

public class Main {
    /**//w  ww  .  j a va 2 s.co  m
     * Get text from the clipboard.
     * @param context the application {@link Context}
     * @return Returns the clipboard text if the clipboard contains text.
     */
    public static String readFromClipboard(Context context) {
        // Gets the ClipboardManager
        ClipboardManager clipboard = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);

        // Gets the clipboard data from the clipboard
        ClipData clip = clipboard.getPrimaryClip();
        if (clip != null) {

            // Gets the first item from the clipboard data
            ClipData.Item item = clip.getItemAt(0);

            CharSequence text = item.getText();
            if (text != null && text.length() > 0) {
                return text.toString();
            }
            // return coerceToText(context, item).toString();
        }

        return null;
    }
}

Related

  1. readFromClipboard(Context context)