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?
Reference : How to copy text programmatically 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");
clipboard.setPrimaryClip(clip);
}
Added: you may have to clean and build your project :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With