I want to make that when I clicked on a TextView in andorid studio it was copied to the clipboard, I tried to put the android:longClickable="true"
line in the text view tag as I read on a page, but nothing.
I have changed in the android documentation and I have tried to put this code in OnClick
of TextView
, but nothing, I do not give any error, but I do not copy it in the clipboard. Surely this is wrong,
Could someone tell me how to do it, either as a tag or as a java code?
public class FragLay extends Activity {
TextView textView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frag_lay);
textView3=(TextView)findViewById(R.id.textView);
textView3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClipboardManager myClipboard = myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
int min = 0;
int max = textView3.getText().length();
if (textView3.isFocused()) {
final int selStart = textView3.getSelectionStart();
final int selEnd = textView3.getSelectionEnd();
min = Math.max(0, Math.min(selStart, selEnd));
max = Math.max(0, Math.max(selStart, selEnd));
final CharSequence selectedText = textView3.getText().subSequence(min, max);
String text = selectedText.toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);
}
}
});
}
}