0

Tried online search already. Here is an example of what i am trying to do:

text within a textview is: "hey how are you doing check this link: http://www.google.com and if you dont like that link try this link http://yahoo.com or finally try http://tinyurl.com/wp-tinyurl"

I would like to make all these links clickable in a listview. I do not want to use android:autoLink="web" on the textview object as the list item itself is clickable and these can consume the click event or cause confusion. Im looking for a way to scan through the text and collect the links and then change them to spanURL this way only the text becomes clickable not the textview. if it makes any difference here is the textview within the row layout of my listview that i have now:

 <TextView
        android:id="@+id/tv_user_response"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25dp"
        android:autoLink="web"
        android:descendantFocusability="blocksDescendants"
        android:textColor="@color/grey_font"
        android:textSize="14sp" />

but i believe i have to handle this programatically.

UPDATE: thanks to the link provided here is what i ended up doing:

public class Linkifier {


public TextView setLinks(TextView tv, String text) {
    String[] linkPatterns = {
            "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])",
            "#[\\w]+", "@[\\w]+" };
    for (String str : linkPatterns) {
        Pattern pattern = Pattern.compile(str);
        Matcher matcher = pattern.matcher(tv.getText());
        while (matcher.find()) {
            int x = matcher.start();
            int y = matcher.end();
            final android.text.SpannableString f = new android.text.SpannableString(
                    tv.getText());
            InternalURLSpan span = new InternalURLSpan();
            span.text = text.substring(x, y);
            f.setSpan(span, x, y,
                    android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.setText(f);
            // tv.setOnLongClickListener(span.l);

        }
    }
    tv.setLinkTextColor(Color.BLUE);
    tv.setLinksClickable(true);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
    tv.setFocusable(false);

    return tv;

}

class InternalURLSpan extends android.text.style.ClickableSpan {
    public String text;

    @Override
    public void onClick(View widget) {
        Utils.createToast(widget.getContext(),text);
        handleLinkClicked(widget.getContext(),text);
    }


    public void handleLinkClicked(Context context,String value) {
        if (value.startsWith("http")) {     // handle http links
        } else if (value.startsWith("@")) {
            // handle @links

        } else if (value.startsWith("#")) { // handle #links
            String searchTerm=text.replace("#", "");

            String query = null;
            try {
                query = URLEncoder.encode(text, "utf-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Consts.URL_TWITTER_SEARCH+query));
            context.startActivity(browserIntent);
        }
    }
}

}

then you can create the linkifier class and i ran it like this: Linkifier linkifier= new Linkifier(); linkifier.setLinks(myTextView, "my message etc");

the textview has to have the text already in it and the 2nd paramter matches on what text your looking for. so if myTextView contained "hey how are you doing check this link: http://www.google.com and if you dont like that link try this link http://yahoo.com or finally try http://tinyurl.com/wp-tinyurl" then the 2nd parameter put the same string.

j2emanue
  • 60,549
  • 65
  • 286
  • 456

0 Answers0