8

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?

JavaRunner
  • 2,455
  • 5
  • 38
  • 52

1 Answers1

27

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 :)

Vishal Vyas
  • 2,571
  • 1
  • 22
  • 39
  • Yeah, I tried this code but it doesn't work. It even doesn't compile because the compiler says: Call requires API level 11 (current min is 7): android.content.ClipboardManager#setPrimaryClip line 245 Android Lint Problem. – JavaRunner Oct 11 '12 at 20:12
  • did you declared something like this `` in manifest file?? – Vishal Vyas Oct 11 '12 at 20:15
  • Sure. The compiler throws an error: "**current** min is 7" (it's my app needs min API7). So my manifest is: – JavaRunner Oct 11 '12 at 20:18
  • I know that the first block of code I can use on devices with Android API <11, but how I can turn off checking code in the second block? – JavaRunner Oct 11 '12 at 20:48
  • actually I din't get you.. is it still giving you compile errors? – Vishal Vyas Oct 11 '12 at 20:54
  • and I think you need to change you project build target to api level 13.. in eclipse just right click your project => properties => Android => select Android 3.2 => ok – Vishal Vyas Oct 11 '12 at 20:58
  • In properties I use Google Api 3.2 (API13). It still giving me compile errors. Compiler tells me that I can't use "android.content.ClipboardManager" because my min version is 7 :) I think it because of my "min 7" but I can't set a value higher than 7 :( – JavaRunner Oct 11 '12 at 21:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17896/discussion-between-vishal-vyas-and-javarunner) – Vishal Vyas Oct 11 '12 at 21:05
  • thank you so much! I forget to clean and rebuild my project. After that all works fine! Thanks! – JavaRunner Oct 11 '12 at 21:15
  • 2
    You can give an annotation `@TargetApi(11)` before the function that does this to tell the compiler that the method makes proper checks for the api. – midhunhk Jan 18 '13 at 06:14
  • This code will fail on < api 11, because setText was introduced and deprecated in api lvl 11. Or am i missing something? http://developer.android.com/reference/android/content/ClipboardManager.html#setText%28java.lang.CharSequence%29 – Diego Frehner Jun 30 '14 at 13:05
  • About "newPlainText", what's really the use of "text label" ? When do you get to see it? Also, is there a way to invoke the default, built-in toast of copying to clipboard ("copied to clipboard") ? – android developer Feb 07 '16 at 11:00