Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid -Convert large paragraph links to spanURL
    primarykey
    data
    text
    <p>Tried online search already. Here is an example of what i am trying to do:</p> <p>text within a textview is: "hey how are you doing check this link: <a href="http://www.google.com" rel="nofollow">http://www.google.com</a> and if you dont like that link try this link <a href="http://yahoo.com" rel="nofollow">http://yahoo.com</a> or finally try <a href="http://tinyurl.com/wp-tinyurl" rel="nofollow">http://tinyurl.com/wp-tinyurl</a>" </p> <p>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:</p> <pre><code> &lt;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" /&gt; </code></pre> <p>but i believe i have to handle this programatically.</p> <p>UPDATE: thanks to the link provided here is what i ended up doing:</p> <pre><code>public class Linkifier { public TextView setLinks(TextView tv, String text) { String[] linkPatterns = { "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\"&gt;\\]\\)]*[^\\. ,'\"&gt;\\]\\)])", "#[\\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; </code></pre> <p>}</p> <pre><code>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); } } } </code></pre> <p>}</p> <p>then you can create the linkifier class and i ran it like this: Linkifier linkifier= new Linkifier(); linkifier.setLinks(myTextView, "my message etc");</p> <p>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: <a href="http://www.google.com" rel="nofollow">http://www.google.com</a> and if you dont like that link try this link <a href="http://yahoo.com" rel="nofollow">http://yahoo.com</a> or finally try <a href="http://tinyurl.com/wp-tinyurl" rel="nofollow">http://tinyurl.com/wp-tinyurl</a>" then the 2nd parameter put the same string. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload