android – I want text view as a clickable link-ThrowExceptions

Exception or error:
<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.

How to solve:

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>

###

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

###

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"

###

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.

###

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!

###

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

###

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));
    }

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *