Last active
October 29, 2021 19:04
-
-
Save webserveis/25b82e519da6dd89efb088ae1fa86c64 to your computer and use it in GitHub Desktop.
Clipboard Utils for Java and Kotlin Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.webserveis.basewebtool.utils; | |
import android.content.ClipData; | |
import android.content.ClipboardManager; | |
import android.content.Context; | |
import android.widget.Toast; | |
import com.webserveis.basewebtool.R; | |
import androidx.annotation.Nullable; | |
public class ClipboardUtils { | |
public static String getClipboardText(final Context context) { | |
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | |
final ClipData clipData = clipboard.getPrimaryClip(); | |
if (clipData != null && clipData.getItemCount() > 0) { | |
final CharSequence clipboardText = clipData.getItemAt(0).getText(); | |
if (clipboardText != null) { | |
return clipboardText.toString(); | |
} | |
} | |
return null; | |
} | |
public static void setClipboardText(final Context context, @Nullable final String label, @Nullable final String text) { | |
final ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | |
final ClipData clipData = ClipData.newPlainText(label, text); | |
clipboard.setPrimaryClip(clipData); | |
} | |
public static void setClipboardText(final Context context, @Nullable final String label, @Nullable final String text, boolean notify) { | |
setClipboardText(context, label, text); | |
if (notify) { | |
Toast.makeText(context, String.format(context.getText(R.string.clipboard_copy).toString(), label), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ClipboardUtils { | |
fun setText(context: Context, text: String?, label: String = "clip_text"): Boolean { | |
val myClipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | |
myClipboard.setPrimaryClip(ClipData.newPlainText(label, text)) | |
return true | |
} | |
fun getText(context: Context): String? { | |
val myClipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | |
val clipData = myClipboard.primaryClip | |
if (clipData != null && clipData.itemCount > 0) { | |
val clipboardText = clipData.getItemAt(0).text | |
if (clipboardText != null) { | |
return clipboardText.toString() | |
} | |
} | |
return null | |
} | |
} | |
//use | |
if (ClipboardUtils.setText(requireContext(), mViewModel.spoilerOutput)) | |
requireContext().toast(getText(R.string.spoiler_preview_copy_toast)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<string name="clipboard_copy">%s copied to clipboard</string> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment