2

I am facing a strange problem after putting all the effort I am still unable to solve it. The problem is: I have a textview which is loading data directly from Twitter. Tweets may contains many links and it is getting displayed in the textview, but on clicking links, they are opening in android default browser. What I want to do is when user click any of the link how will I detect which link has been clicked and open my webview instead of default browser.

Here what I have reached until now:

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello www.google.com Thank You"
    android:autoLink="web"
    android:id="@+id/activity_main_textview"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Java

TextView textView = (TextView) findViewById(R.id.activity_main_textview);
textView.setText("www.google.com is first link, www.facebook.com is second link, www.instagram.com is third link");
textView.setMovementMethod(LinkMovementMethod.getInstance());
Ravi
  • 960
  • 1
  • 18
  • 41
  • did you check https://stackoverflow.com/questions/25629691/open-activity-with-linkify-android? – Mohammad Tabbara Mar 21 '18 at 14:12
  • 1
    Possible duplicate of [Android TextView with Clickable Links: how to capture clicks?](https://stackoverflow.com/questions/12418279/android-textview-with-clickable-links-how-to-capture-clicks) – leonardkraemer Mar 21 '18 at 14:12

1 Answers1

0

In the Android manifest inside the activity tag where the WebView reside add:

<activity android:name=".WebViewActivity">
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="http"
        android:host="*" />

</intent-filter>
</activity>

On the TextView I did:

<TextView
    android:id="@+id/link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="http://www.google.com"
    android:autoLink="web"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

This worked for me.

Mohammad Tabbara
  • 1,438
  • 1
  • 16
  • 31