3

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

<TextView
    android:id="@+id/text"
    android:text="abcdefgh@def.com"
    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="abcdefgh@def.com"
        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();
    }
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224

4 Answers4

1

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

Ahmad Ayyaz
  • 774
  • 8
  • 25
1

Use autoLink="web".

<TextView
    android:id="@+id/text"
    android:text="abcdefgh@def.com"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="web" />

To do it programmatically, use view.setMovementMethod(LinkMovementMethod.getInstance()); in your code and make sure you do not have android:autoLink="web" in your XML layout.

Example:

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

EDIT

Use android:linksClickable="true" also. It might work. Also make sure to use either autoLink or setMovementMethod. Do not use both of them together.

EDIT 2

I realise you want to use performClick() on your textView. Although there aren't many examples for android.widget.TextView.performClick(), which means the method is either unpopular or old. However it can be implemented like this:

TextView text ;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (TextView)findViewById(R.id.Text1);
    text.setOnClickListener(this);
}

public void onClick(View arg0) {
      Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com"));
        startActivity(i);
    }
}

This matches with your requirement. Check if it works.

Abhi
  • 3,431
  • 1
  • 17
  • 35
  • Thanks, but it didn't help me, please, see an update. – CoolMind Apr 05 '18 at 11:52
  • I tried to add `android:linksClickable="true"` and remove `text.setMovementMethod(LinkMovementMethod.getInstance());`, but it didn't help, sorry. – CoolMind Apr 05 '18 at 12:21
  • Thanks. Similar answer offered @PAWAN LAKHOTIA. I think if add some actions on `onClick` event (for URL, email, phone) it will work. – CoolMind Apr 05 '18 at 14:32
1

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;
 }
Pawan Lakhotia
  • 385
  • 1
  • 10
  • Sorry, where can I override it? – CoolMind Apr 05 '18 at 12:17
  • 1
    sorry, do this- `add this android:onClick="onClick"` then You must also implement View.OnClickListener and in onClick method can use intent Intent intent = new Intent(android.content.Intent.ACTION_VIEW); intent.setData(Uri.parse("https://youraddress.com")); startActivity(intent); I have tested this,it worked fine – Pawan Lakhotia Apr 05 '18 at 12:30
  • or to click on a piece of the text (not the whole TextView), you can use Html or Linkify (both create links that open urls, though, not a callback in the app). – Pawan Lakhotia Apr 05 '18 at 12:33
  • Probably it's a good solution, but it fails with exception (`startActivity` line): "Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=youraddress.com }". – CoolMind Apr 05 '18 at 13:17
  • To solve it, we may write a correct URL like: `intent.setData(Uri.parse("https://www.facebook.com/abc/"));`. – CoolMind Apr 05 '18 at 13:27
  • Change your answer, please, so that I could accept it. I mean write `textView.setOnClickListener` with URL, email, phone behaviour, also see exception catch for them (https://stackoverflow.com/questions/5882656/no-activity-found-to-handle-intent-android-intent-action-view, https://stackoverflow.com/questions/11517563/no-activity-found-to-handle-intent-while-emailing-data, etc). – CoolMind Apr 05 '18 at 13:39
1

Unfortunately, the thing you are trying to achieve is not possible. Based on documentation of textView.performClick():

Call this view's OnClickListener, if it is defined

Since you haven't defined any onClickListener, the approaches won't work.

The workaround would be to define your custom onClickListener and try to parse the URI programatically to trigger appropriate Intent.ACTION_VIEW

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • Oh! Thanks. So much time has been lost. I will create a listener as PAWAN LAKHOTIA and Abhi offered. – CoolMind Apr 05 '18 at 14:46
  • Yes. You can create a listener. But be careful, the approaches mentioned might not work for link type `PHONE_NUMMBER`. You might need to parse the content carefully and trigger `ACTION_VIEW` or `ACTION_CALL` – Sagar Apr 05 '18 at 14:53
  • 1
    Great! Enjoy coding – Sagar Apr 05 '18 at 14:57