If you want to Copy a Text to Clipboard on a Android device Programatically, you will have to use android.text.ClipboardManager for Older Device (API level below 11), but if you want to support API level's 11 and above you need to use android.content.ClipboardManager.
As android.text.ClipboardManager is being deprecation since API level 11 , the warning needs to be suppressed.
Below is the code to handle Clipboard Copy for all android Devices.
//Get the Operation System SDK version as an int
int sdkVer = android.os.Build.VERSION.SDK_INT;
//For Older Android SDK versions
if(sdkVer < android.os.Build.VERSION_CODES.HONEYCOMB) {
@SuppressWarnings("deprecation")
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText("Text Copied to Clipboard");
}
//For Newer Versions
else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Message","Text copied to Clipboard");
clipboard.setPrimaryClip(clip);
}
More Posts related to Android,
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
More Posts:
- Best way to Convert Primitive long to int in Java with some Cautions - Java
- Android Disable EditText from Auto Focus on Activity load - Android
- How to force quit or kill Notepad++ Process - NotepadPlusPlus
- How to show Videos on SharePoint Page - SharePoint
- [fix] docker exec requires at least 2 arguments - Docker
- How to check of Updates on Microsoft Edge Browser on Mac (macOS) - MacOS
- Java JDBC NumberFormatException: For input string - Java
- Change Home Page on Safari for Mac (macOS) - MacOS