Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolink inside a TextView in android

Tags:

How to give autolink for some part of textview ? For example : My text inside TextView is "Please click here to open this webpage". I want to show link for only the text "here". And I should open that webpage onclick of the text "here" but not on the anywhere of TextView.

like image 927
Rahul Baradia Avatar asked Apr 20 '12 12:04

Rahul Baradia


People also ask

What is autoLink in android?

andorid:autoLink => By using this attribute, android can controls whether links(such as urls, emails, phone and address) are automatically found and converted to clickable links.

Can we Nest TextView inside TextView?

In Android all layout can be nested one another.

What is EMS in TextView?

ems is a unit of measurement. The name em was originally a reference to the width of the capital M. It sets the width of a TextView/EditText to fit a text of n 'M' letters regardless of the actual text extension and text size. Eg : android:ems Makes the EditText be exactly this many ems wide.


1 Answers

Put a String in string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="txtCredits">Support: <a href="http://www.stackoverflow.com">click here</a></string>
</resources>

And you can use it in textView like this:

<TextView
        android:layout_width="fill_parent"
        android:id="@+id/text"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:linksClickable="true"
        android:text="@string/txtCredits" />

EDIT

For some reason above code does not work properly. So, add below code also,

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

Important: Don't forget to remove autoLink="web" if you are calling setMovementMethod().

like image 120
asish Avatar answered Sep 30 '22 09:09

asish