将光标移到/点击文章中的句子上,可以查看译文。      显示繁体中文内容    显示简体中文内容

How do I make links in a TextView clickable?
一个TextView 如何使用可单击的链接?

I have the following TextView defined :


<TextView android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:text="@string/txtCredits"
 android:autoLink="web" android:id="@+id/infoTxtCredits"
 android:layout_centerInParent="true"
 android:linksClickable="true"></TextView>

where @string/txtCredits is a string resource that contains<a href="some site">Link text</a>

Android is highlighting the links in the TextView, but they do not respond to clicks.can someone tell me what i'm doing wrong?do i have to set an onClickListener for the TextView in my activity for something as simple as this?

Looks like it has to do with the way i define my string resource.this does not work :


<string name="txtCredits"><a href="http://www.google.com">Google</a></string>

But this does :


<string name="txtCredits">www.google.com</string>

Which is a bummer because i would much rather show a text link than show the full URL.

时间:

Buried in the API demos i found the solution to my problem :

Link.java :


//text2 has links specified by putting <a> tags in the string
//resource. By default these links will appear but not
//respond to user input. To make them active, you need to
//call setMovementMethod() on the TextView object.

 TextView t2 = (TextView) findViewById(R.id.text2);
 t2.setMovementMethod(LinkMovementMethod.getInstance());

I removed most of the attributes on my TextView to match what was in the demo.


<TextView
 android:id="@+id/text2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/imgCredits"
 android:layout_centerInParent="true"
 android:layout_marginTop="20dp"
 android:text="@string/txtCredits"/>

That solved it. Pretty difficult to uncover and fix.

I'm using only android:autoLink="web" and it works fine.A click on the link opens the browser and shows the correct page.

One thing i could guess is that some other view is above the link.something that is transparent fills the whole parent but don't displays anything above the link.in this case the click goes to this view instead of the link.

The above solutions didn't work for me, but the following did (and it seems a bit cleaner).
First, in the string resource, define your tag opening chevrons using the HTML entity encoding, i.e. :


&lt;a href="http://www.google.com">Google&lt;/a>

and NOT :


<a href="http://www.google.com">Google</a>

In general, encode all the chevrons in the string like that.BTW, the link must start with http://

Then (as suggested here ) set this option on your TextView :


 android:linksClickable="true"

Finally, in code, do :


((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));

That's it, no regexes or other manual hacks required.

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 overrideview.setMovementMethod(LinkMovementMethod.getInstance());(i.e.; links of the third kind will be highlighted, but not clickable).

The moral of the story is useview.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.

If you want to add HTML-like link, all you need to do is :

  • add a resource HTML-like string :

    
     <string name="link"><a href="https://www.google.pl/">Google</a></string>
    
    
  • add your view to the layout with no link-specific configuration at all :

    
     <TextView
     android:id="@+id/link"
     android:text="@string/link"/>`
    
    
  • add appropriate MovementMethod programmatically to your TextView :

    
     mLink = (TextView) findViewById(R.id.link);
     if (mLink!= null) {
     mLink.setMovementMethod(LinkMovementMethod.getInstance());
     }
    
    

That's it! and yes, having options like"autoLink"and"linksClickable"working on explicit links only (not wrapped into html tags) is very misleading to me too...

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"/>

All tested and working 100%
Solution : android:autoLink="web"
below is a complete example

Sample Layout Xml


 <TextView
 android:id="@+id/txtLostpassword"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:autoLink="email"
 android:gravity="center"
 android:padding="20px"
 android:text="@string/lostpassword"
 android:textAppearance="?android:attr/textAppearanceSmall"/>

 <TextView
 android:id="@+id/txtDefaultpassword"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:autoLink="web"
 android:gravity="center"
 android:padding="20px"
 android:text="@string/defaultpassword"
 android:textAppearance="?android:attr/textAppearanceSmall"/>

String in string.xml


<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>

Richard, next time, you should add this code under TextView at the layout XML instead.


android:autoLink="all"

This should be like this.


<TextView 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
 android:text="@string/txtCredits"
 android:id="@+id/infoTxtCredits"
 android:autoLink="all"
 android:linksClickable="true">
</TextView>

You don't need to use this code (t2.setMovementMethod(LinkMovementMethod.getInstance());) in order to make the link clickable.

Also, here's the truth : as long as you set the autoLink and the linksClickable, don't forget to add this at String.xml file so that the clickable link will work.


<string name="txtCredits"><a href="http://www.google.com">Google</a></string>

i hope this will help you;


String value ="<html>Visit my blog <a href="http://www.maxartists.com">mysite</a> View <a href="sherif-activity://myactivity?author=sherif&nick=king">myactivity</a> callback</html>";
 TextView text = (TextView) findViewById(R.id.text);


 text.setText(Html.fromHtml(value));
 text.setMovementMethod(LinkMovementMethod.getInstance());

i used this simply


Linkify.addLinks(TextView, Linkify.ALL);

makes the links clickable given here

...