Reputation: 183
I want to open link which is in text view in the form of text:type ="autolink" and it contains some URL but when a user tap on it, opens browser instead of webview activity
For Example
{
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="web"
android:text="www.facebook.com"
android:id="@+id/iimage" />
}
so please tell if a user clicks on this link it opens in webView in WebView activity. How to do this
Upvotes: 0
Views: 861
Reputation: 2135
You can do this simply by:
iimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(currentactivity, youractivity.class);
intent.putExtra("url", your_url );
startActivity(intent);
finish();
}
});
Inside onCreate of your youractivity
String url = getIntent().getStringExtra("url");
webView.loadUrl(url);
Hope this helps.
Upvotes: 0
Reputation: 600
You will need to set a Spannable to your textview and set the click listener on that Spannable.
Here you have an example i took from some answer: Android: ClickableSpan in clickable TextView
TextView tv = (TextView)findViewById(R.id.textview01);
Spannable span = Spannable.Factory.getInstance().newSpannable("test link span");
span.setSpan(new ClickableSpan() {
@Override
public void onClick(View v) {
Log.d("main", "link clicked");
Toast.makeText(Main.this, "link clicked", Toast.LENGTH_SHORT).show();
} }, 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// All the rest will have the same spannable.
ClickableSpan cs = new ClickableSpan() {
@Override
public void onClick(View v) {
Log.d("main", "textview clicked");
Toast.makeText(Main.this, "textview clicked", Toast.LENGTH_SHORT).show();
} };
// set the "test " spannable.
span.setSpan(cs, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set the " span" spannable
span.setSpan(cs, 6, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(span);
tv.setMovementMethod(LinkMovementMethod.getInstance());
Upvotes: 1
Reputation: 82
Look How can we open TextView's links into Webview
But actually all you need is webView.loadUrl("www.facebook.com")
Upvotes: 0