If you want to copy text from edittext So First Create Edittext
EditText mResultEt = findViewById(R.id.resultEt);
then Create One Button on which after clicking we can copy these text
ImageButton copyClipBoard = findViewById(R.id.btn_copy);
then use the listener of the button
Java
copyClipBoard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboardManager = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clipData = ClipData.newPlainText("nonsense_data",
mResultEt.getText().toString());
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(MainActivity.this, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
}
});
Kotlin
btn1.setOnClickListener{
val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(
"nonsense_data",
content_et.getText().toString()
)
clipboardManager.setPrimaryClip(clipData)
Toast.makeText(this@MainActivity, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show()
}
and make sure to import this
import android.content.ClipboardManager;
don't import this one
android.text.ClipboardManager