I have textview which gets populated by multiple links sometimes and sometimes a single link. When user clicks on one link it should exactly open same link in Activity which has webview.
String link = "http://www.google.com\">http://www.google.com"; String link1 = "http://www.google.com\">http://www.google.com http://www.facebook.com\">http://www.facebook.com";
I use following reference to implement custom link movement method but it still opens in browsers.
However it still opens in browser:
Following is my code:
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.testtv);
textView.setText(Html
.fromHtml("<a href=\"http://www.google.com\">http://www.google.com</a>"));
textView.setMovementMethod(CustomLinkMovementMethod.getInstance());
}
Main activity xml:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.textviewtest.MainActivity" >
<TextView
android:id="@+id/testtv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textAppearance="@android:style/TextAppearance.Small" />
</RelativeLayout>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.textviewtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".WebViewActivity"
android:label="@string/title_activity_web_view" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
</activity>
</application>
</manifest>
CustomLinkMovementMethod class as taken from above reference:
package com.example.textviewtest;
import android.content.Context;
import android.content.Intent;
import android.text.Layout;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;
public class CustomLinkMovementMethod extends LinkMovementMethod {
private static Context movementContext;
private static CustomLinkMovementMethod linkMovementMethod = new CustomLinkMovementMethod();
public boolean onTouchEvent(android.widget.TextView widget,
android.text.Spannable buffer, android.view.MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length != 0) {
String url = link[0].getURL();
if (url.startsWith("https") || url.startsWith("http")) {
Log.d("Link", url);
Toast.makeText(movementContext, "Link was clicked",
Toast.LENGTH_LONG).show();
movementContext.startActivity(new Intent(movementContext,
WebViewActivity.class));
} else if (url.startsWith("tel")) {
Log.d("Link", url);
Toast.makeText(movementContext, "Tel was clicked",
Toast.LENGTH_LONG).show();
} else if (url.startsWith("mailto")) {
Log.d("Link", url);
Toast.makeText(movementContext, "Mail link was clicked",
Toast.LENGTH_LONG).show();
}
return true;
}
}
return super.onTouchEvent(widget, buffer, event);
}
public static android.text.method.MovementMethod getInstance(Context c) {
movementContext = c;
return linkMovementMethod;
}
}
I want to handle this and open specific link opened in textview in activity with webview?