clip text to Clipboard and set the plain text - Android Android OS

Android examples for Android OS:Clipboard

Description

clip text to Clipboard and set the plain text

Demo Code


//package com.java2s;
import android.content.Context;
import android.support.annotation.NonNull;

public class Main {
    public static void clip(@NonNull Context context, @NonNull 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);/*from  w w  w.  j a va  2 s .  co m*/
        } else {
            android.content.ClipboardManager clipboard = (android.content.ClipboardManager) context
                    .getSystemService(Context.CLIPBOARD_SERVICE);
            android.content.ClipData clip = android.content.ClipData
                    .newPlainText("FinestWebView", text);
            clipboard.setPrimaryClip(clip);
        }
    }
}

Related Tutorials