1

I am trying to make text in TextView clickable (allow copy to clipboard) and links also clickable but without any success.

Here is my MainActivity.xml code:

android:enabled="true"
android:textIsSelectable="true"
android:focusable="true"
android:longClickable="true"
android:linksClickable="true"

Here is the MainActivity Kotlin file:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Any help would be appreciated.

Zankrut Parmar
  • 1,872
  • 1
  • 13
  • 28
k4li
  • 39
  • 4

3 Answers3

1

just add onclicklistner to your textview.

For your reference

 textView.setOnClickListener((View view) -> {
          //Your code
        });
Nihthiya Althaf
  • 202
  • 1
  • 10
0

Use the following code so when you click on a TextView, the text will be copied to the clipboard :

myTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData clip = ClipData.newPlainText("copy", myTextView.getText().toString());
                clipboard.setPrimaryClip(clip);
                Toast.makeText(MainActivity.this, "Copied To Clipboard", Toast.LENGTH_SHORT).show();


            }
        });
Hiraeths
  • 380
  • 3
  • 16
  • Where did you put the code? Can you share the logcat when the app crashes? – Hiraeths Oct 07 '20 at 01:11
  • Thanks for your support, i put the code on the MainActivity.kt the android studio as changed the code to klotin the logcat its big there is the last lines let me know if you need the whole log: – k4li Oct 07 '20 at 11:47
  • You forgot, to declare your text view before your setOnClickListener. val postCaption = findViewById(R.id.myTextView). – Hiraeths Oct 08 '20 at 01:20
0

Try this

<TextView
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:id="@+id/no"
     style="?android:attr/borderlessButtonStyle"
 />
Praveen Kumar
  • 329
  • 2
  • 14