Example usage for android.content ClipboardManager getPrimaryClip

List of usage examples for android.content ClipboardManager getPrimaryClip

Introduction

In this page you can find the example usage for android.content ClipboardManager getPrimaryClip.

Prototype

public @Nullable ClipData getPrimaryClip() 

Source Link

Document

Returns the current primary clip on the clipboard.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static String getText(Context context) {
    final ClipboardManager clipboard = getClipboard(context);
    final ClipData clipData = clipboard.getPrimaryClip();

    return clipData.getItemAt(0).getText().toString();
}

From source file:Main.java

public static String getClipData(Context context) {
    ClipboardManager clip = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    return clip.getPrimaryClip().getItemAt(0).getText().toString();
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static int getItemCount(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData data = clipboard.getPrimaryClip();
    return data.getItemCount();
}

From source file:Main.java

public static CharSequence getText(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = clipboard.getPrimaryClip();
    if (clip != null && clip.getItemCount() > 0) {
        return clip.getItemAt(0).coerceToText(context);
    }/*  w w w  .j  ava  2s.  c o  m*/
    return null;
}

From source file:Main.java

@NonNull
public static String paste(Context context) {
    ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = manager.getPrimaryClip();
    ClipData.Item item = clipData.getItemAt(0);
    return item.getText().toString();
}

From source file:Main.java

public static Uri getUri(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = clipboard.getPrimaryClip();
    if (clip != null && clip.getItemCount() > 0) {
        return clip.getItemAt(0).getUri();
    }/* ww  w.  j  av  a2 s.  c om*/
    return null;
}

From source file:Main.java

private static String getClipText(Context context) {
    String text = "";
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = cm.getPrimaryClip();
    if (clipData != null) {
        if (clipData.getItemCount() > 0) {
            ClipData.Item item = clipData.getItemAt(0);
            text = item.getText().toString();
        }/*from  w  w w .  j a  v  a  2 s.c  om*/
    }

    return text;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static String getText(Context context, int index) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = clipboard.getPrimaryClip();
    if (clip != null && clip.getItemCount() > index) {
        return String.valueOf(clip.getItemAt(0).coerceToText(context));
    }//from  ww  w. j a v  a2 s . c  o m
    return null;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static String getLatestText(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = clipboard.getPrimaryClip();
    if (clip != null && clip.getItemCount() > 0) {
        return String.valueOf(clip.getItemAt(0).coerceToText(context));
    }/*from   www .  ja  v a  2s .  c o  m*/
    return null;
}

From source file:Main.java

/**
 * This is only called when we are on API level > Honeycomb
 * @param context The context, required to get the Cliboard System Service. 
 * @return Returns the latest item added to the clipboard by the user, as long as the {@code MimeType} is {@code MIMETYPE_TEXT_PLAIN}
 *//*from   w  w  w  .  ja  v  a2s  . co m*/
@SuppressLint("NewApi")
public static String getItemFromCliboard(final Context context) {
    ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    final ClipData data = manager.getPrimaryClip();

    if (data == null) { //If no data on clipboard, let's call it a day. 
        return EMPTY_STRING;
    }

    final String clipItem;

    if (data.getDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
        ClipData.Item item = data.getItemAt(0);
        clipItem = item.getText().toString();
    }

    else {
        clipItem = EMPTY_STRING;
    }

    return clipItem;

}