Example usage for android.content ClipboardManager setPrimaryClip

List of usage examples for android.content ClipboardManager setPrimaryClip

Introduction

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

Prototype

public void setPrimaryClip(@NonNull ClipData clip) 

Source Link

Document

Sets the current primary clip on the clipboard.

Usage

From source file:Main.java

@SuppressLint("NewApi")
public static void copyToClipboard(Context context, String message) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clip = ClipData.newPlainText("", message);
        clipboard.setPrimaryClip(clip);
    } else {/*from ww w .j  a va  2  s . c  o m*/
        clipboard.setText(message);
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static void copyToClipboard11AndLater(@NonNull Context context, @NonNull String toCopy) {
    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", toCopy);
    clipboard.setPrimaryClip(clip);
}

From source file:Main.java

@SuppressLint("NewApi")
public static void copyToClipboard(Context context, String label, String text) {
    if (isHoneycombOrHigher()) {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        android.content.ClipData clip = android.content.ClipData.newPlainText(label, text);
        clipboard.setPrimaryClip(clip);
    } else {//  ww  w .  j a  v  a2 s.  c om
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    }
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static void setText(Context context, CharSequence sequence) {
    ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipData clip = ClipData.newPlainText("ClipboardManagerUtil", sequence);
        clipboardManager.setPrimaryClip(clip);
    } else {// ww  w  . j a  v a 2s. c o m
        clipboardManager.setText(sequence);
    }
}

From source file:com.btmura.android.reddit.app.MenuHelper.java

/**
 * Sets a plain text {@link ClipData} with the provided label and text to the
 * clipboard and shows a toast with the text.
 *//*w  w  w.ja va2 s.c o m*/
public static void copyUrl(Context context, CharSequence label, CharSequence text) {
    context = context.getApplicationContext();
    ClipboardManager cb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    cb.setPrimaryClip(ClipData.newPlainText(label, text));
    Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}

From source file:org.linphone.compatibility.ApiElevenPlus.java

public static void copyTextToClipboard(Context context, String msg) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("Message", msg);
    clipboard.setPrimaryClip(clip);
}

From source file:Main.java

public static void clip(Context context, String text) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setText(text);/*  w ww.ja  va  2s .  co  m*/
    } else {
        ClipboardManager clipboardManager = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setPrimaryClip(ClipData.newPlainText("mq_content", text));
    }
}

From source file:Main.java

public static void clip(Context context, String text) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setText(text);//from www .  j  a va 2  s.  c o  m
    } else {
        ClipboardManager clipboardManager = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboardManager.setPrimaryClip(ClipData.newPlainText("content", text));
    }
}

From source file:Main.java

/**
 * This is only called when oldAPi is passed as false on {@link #addItemToClipboard(Context, String, String, boolean)}
 * @param context The context, required to get the Cliboard System Service. 
 * @param label The label to show to the user via {@code ClipDescription}
 * @param value The value to store on the clipboard. 
 *//*from  w  ww  . j  a  va  2  s . com*/
@SuppressLint("NewApi")
private static void addItemToClipboard(final Context context, final String label, final String value) {
    ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData.Item item = new Item(value);
    ClipDescription description = new ClipDescription(label,
            new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN });
    ClipData data = new ClipData(description, item);
    manager.setPrimaryClip(data);
}

From source file:fr.free.nrw.commons.Utils.java

public static void copy(String label, String text, Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText(label, text);
    clipboard.setPrimaryClip(clip);
}