Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView performClick() doesn't click a link

I have a TextView with android:autoLink="all":

<TextView
    android:id="@+id/text"
    android:text="[email protected]"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="all" />

For some reason I want to open a link set in android:text (it may be phone number, email, etc.). When I run an application, a performClick() doesn't open the link.

TextView textView = findViewById(R.id.text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.performClick();

Linkify.addLinks(buffer, Linkify.ALL); and some others don't help.

UPDATE

Thank you for replies. Show my code after receiving 3 answers. I use API 19, 25 emulators and device (API 21). Sadly, nothing works.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text"
        android:text="[email protected]"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:autoLink="web|email|phone" />

</android.support.constraint.ConstraintLayout>

MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = findViewById(R.id.text);
        int mask = Linkify.ALL;
        Linkify.addLinks(text, mask);
        text.setMovementMethod(LinkMovementMethod.getInstance());
        text.performClick();
    }
}
like image 805
CoolMind Avatar asked Jan 22 '26 04:01

CoolMind


2 Answers

Just use this android:autoLink="web" in your xml and nothing else. It worked for me. And let me know if it works

like image 181
Ahmad Ayyaz Avatar answered Jan 23 '26 18:01

Ahmad Ayyaz


we need to override performClick():

@Override
 public boolean performClick() {
  // Calls the super implementation, which generates an AccessibilityEvent
        // and calls the onClick() listener on the view, if any
        super.performClick();

        // Handle the action for the custom click here

        return true;
 }
like image 26
Pawan Lakhotia Avatar answered Jan 23 '26 16:01

Pawan Lakhotia