copy To Clipboard for sdk version 11 and newer api - Android android.content

Android examples for android.content:Clipboard

Description

copy To Clipboard for sdk version 11 and newer api

Demo Code

import android.annotation.TargetApi;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

public class Main{

    public static void copyToClipboard(Context context, String text) {
        if (Build.VERSION.SDK_INT < 11) {
            android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setText(text);/*from  ww  w .  j  a v a2 s.  c  o  m*/
        } else {
            ClipboardManager clipboard = (ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
        }
    }

}

Related Tutorials