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?
- Do I need to use a completely different but single code that support the API levels that I want?
- Or is there something that I can do so that devices with API 10 will use a different code to copy text?
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.