Reputation: 481
Hi I want to make links from TextView of my app open the WebView Activity
also inside my app.
I am able to handle link clicks using a custom class extending LinkMovementMethod
class. Below is the code:
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.contains("https"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "https Link was clicked", Toast.LENGTH_LONG).show();
} else if (url.contains("tel"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "Tel was clicked", Toast.LENGTH_LONG).show();
} else if (url.contains("mailto"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "Mail link was clicked", Toast.LENGTH_LONG).show();
} else if (url.contains("http"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "http Link was clicked", Toast.LENGTH_LONG).show();
}else if (url.contains("www"))
{
Log.d("Link", url);
Toast.makeText(movementContext, "www 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;
}
}
here is the layout for my MainActivity
<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/textView"
android:linksClickable="true"
android:autoLink="all"
android:text="@string/hello_world" />
</RelativeLayout>
And this is my MainActivity class
public class MainActivity extends Activity {
TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
textView.setMovementMethod(CustomLinkMovementMethod.getInstance(MainActivity.this));
//textView.setMovementMethod(LinkMovementMethod.getInstance());
String customHtml2 ="<div>not valid => <a href='http://facebook.com'>http://facebook</a></div>" +
" <div><a href='http://www.facebook.com'>http://www.facebook.com</a></div>" +
" <div>http://www.google.com <= a regular link<div>" +
" <div>www.google.com <= a regular link<div>" +
" <div><a href='http://google.com'>http://google.com</a></div>" +
" <div><strong style='font-family:Arial, Verdana;font-weight:normal;'>http://www.google.com</strong></div>" +
" <div><strong style='font-family:Arial, Verdana;font-weight:normal;'></strong></div>" +
"";
textView.setText(Html.fromHtml(customHtml2));
}
}
The problem is that the attributes below for the textview makes the onTouchEvent method in CustomLinkMovement class not to be fired and open the default web browser. If I remove them links like this http://www.google.com is not clickable since it is not enclosed in tag.
android:linksClickable="true"
android:autoLink="all"
The problem is I want them to be clickable even if the link is not enclosed in tags and be able to use android:linksClickable="true" and android:autoLink="all"
at the same time.
Upvotes: 4
Views: 5193
Reputation: 157447
use
Linkify.addLinks(yourTextView, Linkify.WEB_URLS);
the only constraints is that your address should start with http://
Upvotes: 2