tags:

views:

454

answers:

4

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 Link text.

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"&gt;Google&lt;/a&gt;&lt;/string&gt;

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.

A: 

Did you set up the permission for android to use the internet in the manifest file?

<?xml version="1.0" encoding="utf-8"?>
<manifest
 xmlns:android="http://schemas.android.com/apk/res/android"
 package="my.app.com"
 android:versionCode="1"
 android:versionName="1">
 <uses-permission
  android:name="android.permission.INTERNET" />
.......

like this?

Jayomat
You only need the internet permission if you want to open the link in your own program to have the link open up in the phone browser after clicking does not require permissions
Janusz
ahhh ok, thank you
Jayomat
A: 

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.

Janusz
Added more info above. Could it be the way I am defining the string as <string name="txtCredits"><a href="http://www.google.com">Google</a></string>? Looks like this is allowed but it's not working for me.
Richard
if you use linkify auto you don't need the a href part. The OS will take the string parse it for urls and converts every url to a clickable link. But this won't result in the word google being linked to google.com. It would display www.google.com as link.
Janusz
A: 

The reason you're having the problem is that it only tries to match "naked" addresses. things like "www.google.com" or "http://www.google.com".

Running your text through Html.fromHtml() should do the trick. You have to do it programatically, but it works.

fiXedd
The links in the TextView are a different color than other text in the string, so I think they're being recognized as links. They're just not clickable :-(
Richard
+4  A: 

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:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/txtCredits"
    android:id="@+id/infoTxtCredits"
    android:layout_below="@+id/imgCredits" android:layout_centerInParent="true"
    android:layout_marginTop="20dp"></TextView>

That solved it. Pretty difficult to uncover and fix.

Richard