Set text as new main clip in the clipboard. - Android Android OS

Android examples for Android OS:Clipboard

Description

Set text as new main clip in the clipboard.

Demo Code


//package com.java2s;

import android.content.Context;
import android.os.Looper;

public class Main {
    /**//from   w ww .j  a v  a2s  .c  o m
     * Set text as new main clip in the clipboard.
     * @param context the application {@link Context}
     * @param lable the label given to the
     * @param text the text for the clipboard
     * @return Returns true if operation has succeeded.
     */
    public static boolean copyToClipboard(Context context, String lable,
            String text) {
        Looper.prepare();
        try {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            android.content.ClipData clip = android.content.ClipData
                    .newPlainText(lable, text);
            clipboard.setPrimaryClip(clip);

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

Related Tutorials