0

I have a TextView in listview which can have 2+ links. I had gone through to this SO link to capture the event of a TextView but this is not working for a list view.

Here is my code:

getView method

if(ann.message.contains("<a href=")){
   setTextViewHTML(holder.announcement, ann.message);
}

methods to make text clickable

protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            // Do something with span.getURL() to handle the link click...
            Log.i("YES-5.0", span.getURL());
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}

protected void setTextViewHTML(TextView text, String html)
{
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for(URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }
    text.setText(strBuilder);
}

This is my TextView:

<TextView
        android:id="@+id/textViewAnnouncementMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="4dp"
        android:focusable="false"
        android:textColor="@android:color/black"
        android:maxLines="5"
        android:text="@string/message"
        android:textSize="16sp"/>

I have tried with all the combinations of following attributes of `TextView' but still no success:

android:autoLink="all"
android:clickable="true"
android:linksClickable="true"
Community
  • 1
  • 1
Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77

3 Answers3

0

if you use a XML Layout for your textview and that your clickable links all start with http:// il devrait etre suffisant d'utiliser les attributs suivants:

        <TextView
            ...      
            android:autoLink="all"
            android:linksClickable="true"
            android:textColorLink="@color/colorAccent" />

autoLink all means that all mailAdress, http link and phone numbers will be clickable. Just give a look at official documentation for further details: http://developer.android.com/reference/android/text/util/Linkify.html#ALL

gbaccetta
  • 4,449
  • 2
  • 20
  • 30
0

Have you set your movement method on your TextView? Also, if your link comes as an a href tag, you can just use Html.fromHtml to build your Spannable for you. For example:

TextView mView = (TextView) findViewById(R.id.text1);
mView.setMovementMethod(LinkMovementMethod.getInstance());
mView.setText(Html.fromHtml(YOUR_LINK_STRING));
Submersed
  • 8,810
  • 2
  • 30
  • 38
  • Yes but the problem is I can not capture the link click with `setMovementMethod`. – Faisal Shaikh Mar 30 '16 at 14:22
  • Html.fromHtml handles placing the ClickableSpans for you, assuming you provide the link text in correct html format. (i.e. opening the link in a browser) – Submersed Mar 30 '16 at 14:24
  • actually I don't want to open the link in browser I want to open the link within app in web view. That is why I want to capture the link click. – Faisal Shaikh Mar 30 '16 at 14:27
0

I solved my problem by adding following line before calling setTextViewHTML():

holder.announcement.setMovementMethod(LinkMovementMethod.getInstance());

So now my getView() method looks like:

if(ann.message.contains("<a href=")){
   holder.announcement.setMovementMethod(LinkMovementMethod.getInstance());
   setTextViewHTML(holder.announcement, ann.message);
}
Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77