Reputation: 135
In my Android app, I happen to use the following code to copy text from a TextView.
buttonCopy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String copyText;
copyText = textBox2.getText().toString();
myClip = ClipData.newPlainText("text", copyText);
myClipboard.setPrimaryClip(myClip);
Toast.makeText(getApplicationContext(), "Copied", Toast.LENGTH_SHORT).show();
}
});
The minimum API level that I set for my app is API level 10. But I understand that
myClip = ClipData.newPlainText("text", copyText);
myClipboard.setPrimaryClip(myClip);
Requires minimum API level 11.
Sure enough, my app users are sending in the following report
java.lang.NoClassDefFoundError: android.content.ClipboardManager
at com.nepali_unicode.nepalityping.MainActivity.onCreate(Unknown Source)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
I want to support older devices with API level 10. So, my question is how do I enable the copy button for those older devices?
I will be grateful for the help. I am a newbie. My background in PHP programming encouraged me to learn Android. I followed Android Development for Beginners course at Udacity. I'm learning by doing. Oh, and I happen to use Android Studio.
Upvotes: 2
Views: 143
Reputation: 1007296
Do I need to use a completely different but single code that support the API levels that I want?
You can use the simpler API of the original android.text.ClipboardManager
, which should work on all API levels. In your case, that would be simply calling setText()
to put text on the clipboard.
Or is there something that I can do so that devices with API 10 will use a different code to copy text?
You can use Build.VERSION.SDK_INT
to see what version of Android you are on and do different things:
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
// do the newer API
}
else {
// do the older API
}
In your specific case here, since your newer code doesn't seem to be doing anything other than what setText()
would, just using setText()
would be simpler. But if you were taking advantage of more elaborate features offered by the newer clipboard API (e.g., supplying variations of the content in different MIME types, like HTML and plain text), then using the above if
test allows you to do the cool stuff on the newer devices and gracefully degrade on the older devices.
Upvotes: 2