I want text view as a clickable link
Asked Answered
R

8

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.

Rejoice answered 26/7, 2013 at 9:22 Comment(1)
Here is the link https://mcmap.net/q/658532/-android-textview-hyperlinkMarshy
T
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:[email protected]?Subject=Lost%20Password" target="_top">[email protected]</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>
Toilette answered 25/2, 2014 at 2:16 Comment(3)
Any idea why the link isn't clickable?Sanctimonious
wasted probably 1hour until i found this. Chat Ai and search engine trash.Accomplice
android:clickable="true" to make it clickableAccomplice
B
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" /> 
Borneol answered 26/7, 2013 at 10:35 Comment(0)
J
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"
Jochebed answered 26/7, 2013 at 9:26 Comment(4)
text view not clickable brother. what can i do now.Rejoice
what exactly u want please explain onceJochebed
already i done what u said above. but now also not clickable. I don't know where was the problem again.Rejoice
notice the single quotes around the string. I didn't try that, but almost edited this answer and then saw those quotes. Other way is described here: https://mcmap.net/q/166386/-html-in-string-resourceDiscredit
C
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!

Carcassonne answered 20/10, 2015 at 7:55 Comment(0)
D
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.

Darien answered 11/9, 2014 at 4:58 Comment(0)
T
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

Thew answered 26/7, 2013 at 9:49 Comment(5)
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 isRejoice
for now as a quick solution try writing "<a href='google.com' >ForGet Password</a>" instead of R.string.link and forget the string.xmlThew
Try this Edited answerThew
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.Rejoice
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 outThew
H
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"/>
Huan answered 19/3, 2014 at 12:37 Comment(0)
S
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));
    }
Showmanship answered 25/11, 2016 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.