21
<TextView
    android:id="@+id/link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Forget password?"
    android:autoLink="all"
    android:linksClickable="true"
    android:textColor="@color/lgreen"
    android:textStyle="italic"
     />

Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me how can i do that as a clickable and move to another link. i tried more times by seeing examples. but i can't. Can some one explain me clearly how to change text view as a clickable link.

Rameshbabu
  • 611
  • 2
  • 7
  • 21

8 Answers8

54

All tested and working 100%
below is a complete example
Solution: android:autoLink="web"
Sample 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/txtLostpassword"
    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>
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Shanewaj
  • 2,078
  • 1
  • 20
  • 16
13

You can add click listener to TextView and start websearch:

textView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view)
    {
         Intent browser= new Intent(Intent.ACTION_VIEW, Uri.parse(PASTE_YOUR_URL_HERE));  
         startActivity(browser);  
    }

});

and xml looks like:

<TextView  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="@string/link_string"  
    android:textSize="14dp"  
    android:autoLink="web" /> 
Karl Gjertsen
  • 4,690
  • 8
  • 41
  • 64
Mathew1990
  • 327
  • 2
  • 17
7

In the string file put this code

<string name="link">'<a href="http://www.www.com" >ForGet Password</a>'</string> 

and in the XML file

android:text="@string/link"
Developer
  • 6,292
  • 19
  • 55
  • 115
5

this is the most easy and understandable code

TextView link = (TextView) findViewById(R.id.hyper);
String linkText = "This is my website <a href='http://programondaspot.blogspot.com'>Programondaspot</a> .";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());

Check out this post!

HAPPY CODING!

Eliasz Kubala
  • 3,836
  • 1
  • 23
  • 28
dondondon
  • 881
  • 8
  • 4
4

In your onCreate() method put this code:

TextView clickableTextLink = (TextView)findViewById(R.id.your_textview_id); clickableTextLink.setMovementMethod(LinkMovementMethod.getInstance());

I tried a number of solutions. But this worked perfectly for me.

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
2

this is the EDITED answer

Hi you can try by replacing this in you layout.xml file

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="visit the site at www.google.com from here"
    android:autoLink="all"
    android:clickable="true"
    android:linksClickable="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

I have tried it and it will surely work. and treats "www.google.com" as web link

dinesh sharma
  • 3,312
  • 1
  • 22
  • 32
  • i added what u said above. but it shows error in the string like this Multiple annotations found at this line: - error: Apostrophe not preceded by \ (in ') - error: Found text "Forget Password?" where item tag is – Rameshbabu Jul 26 '13 at 11:06
  • for now as a quick solution try writing "ForGet Password" instead of R.string.link and forget the string.xml – dinesh sharma Jul 26 '13 at 11:27
  • Try this Edited answer – dinesh sharma Jul 30 '13 at 04:03
  • this is again changed text name only. i want that text as a clickable link. In my code while i click on the forget password means then it will move directly to the webpage as i mentioned it in the text background at string.xml file. Hope u understand. – Rameshbabu Jul 30 '13 at 10:26
  • then boss why you going for that headache keep one button with backgroung="#00000000" and on click of that button call the intent to load your URL in browser. try it out – dinesh sharma Jul 31 '13 at 04:39
1

Just add onClick tag to your TextView which equals the defined function. Just as below

<TextView
  android:id="@+id/link"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:text="Forget password?"
  android:autoLink="all"
  android:linksClickable="true"
  android:textColor="@color/lgreen"
  android:textStyle="italic"
  android:onClick="callFunction"/>
ARN
  • 175
  • 1
  • 1
  • 13
1

Here is simple implementation tested up to Android N

    String termsText = "By registering, you are agree to our";
    String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
    String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
    String allText = termsText + termsLink + privacyLink;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
        ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
    } else {
        ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
        ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
    }
user2837615
  • 296
  • 1
  • 5
  • 12