I have the following TextView defined:
After spending some time with this, I have found that:
android:autoLink="web"
works if you have full links in your HTML. The following will be highlighted in blue and clickable:
- Some text
<a href="http://www.google.com">http://www.google.com</a>
- Some text
http://www.google.com
view.setMovementMethod(LinkMovementMethod.getInstance());
will work with the following (will be highlighted and clickable):
- Some text
<a href="http://www.google.com">http://www.google.com</a>
- Some text
http://www.google.com
- Some text
<a href="http://www.google.com">Go to Google</a>
Note that the third option has a hyperlink, but the description of the link (the part between the tags) itself is not a link. android:autoLink="web"
does NOT work with such links.
android:autoLink="web"
if set in XML will override view.setMovementMethod(LinkMovementMethod.getInstance());
(i.e.; links of the third kind will be highlighted, but not clickable).The moral of the story is use view.setMovementMethod(LinkMovementMethod.getInstance());
in your code and make sure you don't have android:autoLink="web"
in your XML layout if you want all links to be clickable.
Here is very one line android code to make phone and url selectable from textView no matter what is string and what is data. You dont need to use any HTML tags for this.
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("some url is www.google.com phone 7504567890 another url lkgndflg.com ");
// Makes the textView's Phone and URL (hyperlink) select and go.
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
Only what do you need to add this in text view in xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"/>
Don't know if it's worth adding another answer, but just in case...
I had to hunt this down in a couple places but finally got this version of the code to work.
strings.xml:
<string name="name1"><a href="http://www.google.com">link text1</a></string>
<string name="name2"><a href="http://www.google.com">link text2</a></string>
myactivity.xml:
<TextView
android:id="@+id/textview1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/textview2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="5dp" />
myactivty.java (in onCreate()):
TextView tv1 = (TextView)findViewById(R.id.textview1);
TextView tv2 = (TextView)findViewById(R.id.textview2);
tv1.setText(Html.fromHtml(getResources().getString(R.string.name1)));
tv2.setText(Html.fromHtml(getResources().getString(R.string.name2)));
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv2.setMovementMethod(LinkMovementMethod.getInstance());
This will create two clickable hyperlinks with the text link text1
and link text2
which redirect the user to google.
You need only this:
android:autoLink="web"
Insert this line to TextView, that can be clickable with reference to the web. URL address set as a text of this TextView.
Example:
<TextView
android:id="@+id/textViewWikiURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:text="http://www.wikipedia.org/"
android:autoLink="web" />
by using linkify: Linkify take a piece of text and a regular expression and turns all of the regex matches in the text into clickable links
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("http://www.domain.com");
Linkify.addLinks(textView, Linkify.WEB_URLS);
Don't forget to
import android.widget.TextView;