In xml textview paste this code
android:textIsSelectable="true"
Then in java file,
final TextView txtcopypaste = findViewById(R.id.txtcopypaste); // my textview
txtcopypaste.setOnClickListener(new View.OnClickListener() { // set onclick listener to my textview
@Override
public void onClick(View view) {
ClipboardManager cm = (ClipboardManager)getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(txtcopypaste.getText().toString());
Toast.makeText(getApplicationContext(), "Copied :)", Toast.LENGTH_SHORT).show();
}
});
Requirement : Need to copy and paste the text which is in the textview.
OutCome : Using textview , once i clicked the textview. Its automatically copied the text which is in the textview.
Note: While importing clipboardmanager try to prefer
Please prefer text clipboard manager
import android.text.ClipboardManager; // prefer this
try to avoid content clipboard manager
import android.content.ClipboardManager; // Not this