Example usage for android.text ClipboardManager setText

List of usage examples for android.text ClipboardManager setText

Introduction

In this page you can find the example usage for android.text ClipboardManager setText.

Prototype

public abstract void setText(CharSequence text);

Source Link

Document

Sets the contents of the clipboard to the specified text.

Usage

From source file:Main.java

@SuppressWarnings("deprecation")
public static void copyToClipboard(Context context, String content) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (cm != null) {
        cm.setText(content);
    }/*from   w  w  w .j  ava  2s.co  m*/
}

From source file:Main.java

/**
 * Copy a text to the clipboard./*w ww .  j a v a2s  .c  om*/
 * @param context The current context.
 * @param text The text to copy.
 * @param toastMessage The message to show in a Toast notification. If empty or null, does not display notification.
 */
public static void copyTextToClipboard(Context context, String text, String toastMessage) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Activity.CLIPBOARD_SERVICE);
    clipboard.setText(text);

    if ((toastMessage != null) && (toastMessage.length() > 0)) {
        Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
    }
}

From source file:Main.java

/**
 * Copy a text to the clipboard.//  w  ww .  jav a  2 s . co m
 * 
 * @param context
 *            The current context.
 * @param text
 *            The text to copy.
 * @param toastMessage
 *            The message to show in a Toast notification. If empty or null,
 *            does not display notification.
 */
public static void copyTextToClipboard(Context context, String text, String toastMessage) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(text);

    if ((toastMessage != null) && (toastMessage.length() > 0)) {
        Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show();
    }
}

From source file:Main.java

/**
 * //from   ww w .j  av  a  2s  . c o m
 * @param context
 * @param text
 */
public static void copy(Context context, String text) {
    android.text.ClipboardManager cmb = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    cmb.setText(text);
}

From source file:Main.java

public static void copyToClipboardSupport(Context context, String text) {
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(text);
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static boolean setClipboard(Context context, String text) {
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText(text);
    return true;//from   w w  w  .j  a v  a 2s  .  c  om
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static void clearClipboard(Context context) {
    android.text.ClipboardManager manager = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    manager.setText(EMPTY_STRING);
}

From source file:Main.java

/**
 * This is only called when oldAPi is passed as true on {@link #addItemToClipboard(Context, String, String, boolean)}
 * @param context The context, required to get the Cliboard System Service. 
 * @param value The value to store on the clipboard. 
 *///  w w w. j  a va 2 s . co m
@SuppressWarnings("deprecation")
private static void addTextToClipboard(final Context context, final String value) {
    android.text.ClipboardManager manager = (android.text.ClipboardManager) context
            .getSystemService(Context.CLIPBOARD_SERVICE);
    manager.setText(value);
}

From source file:Main.java

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static void copyToClipboard(Context context, String text) {
    // if()//from  w  w  w.  j a  v  a2s  .c  om
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    } else {
        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
    }
}

From source file:Main.java

/**
 * ClipboardUitls to set a plain text into system clipboard
 * //from  ww w . j a  va 2 s  . c o  m
 * @param context  the application context
 * @param label    the User-visible label for the clip data (for android version HONEYCOMB or higher only)
 * @param text     The actual text in the clip.
 */
public static void setPlainText(Context context, String label, String text) {
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setText(text);
    } else {
        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);
    }
}