Home » Android » Android: Copying a text to the clipboard for API < 11

Android: Copying a text to the clipboard for API < 11

Posted by: admin June 15, 2020 Leave a comment


I have a problem with copying a text to the clipboard. I try to copy like this:

android.content.ClipboardManager clipboard = ( android.content.ClipboardManager ) getSystemService(Context.CLIPBOARD_SERVICE ); 
android.content.ClipData clip = android.content.ClipData.newPlainText( "text label", "text to clip" );
clipboard.setPrimaryClip( clip );

But problem is in the compiler which throws:

Call requires API level 11 (current min is 7): android.content.ClipboardManager#setPrimaryClip line 245 Android Lint Problem.

How I can copy a text to the clipboard on android API < 11? If i try to check API version of android before copying – my code even doesn’t compile. Maybe someone knows an answer to this question?

How to&Answers:

Reference : How to copy text programatically in my Android app

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
    android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    clipboard.setText("text to clip");
} else {
    android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); 
    android.content.ClipData clip = android.content.ClipData.newPlainText("text label","text to clip");

you may have to clean and build your project 🙂