k4li
k4li

Reputation: 39

How to make a TextView clickable in android

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.

Upvotes: 1

Views: 1990

Answers (3)

Praveen Kumar
Praveen Kumar

Reputation: 339

Try this

<TextView
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:id="@+id/no"
     style="?android:attr/borderlessButtonStyle"
 />

Upvotes: 0

Hiraeths
Hiraeths

Reputation: 390

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();


            }
        });

Upvotes: 0

Nihthiya Althaf
Nihthiya Althaf

Reputation: 201

just add onclicklistner to your textview.

For your reference

 textView.setOnClickListener((View view) -> {
          //Your code
        });

Upvotes: 1

Related Questions