url - Android -Convert large paragraph links to spanURL -


tried online search already. here example of trying do:

text within textview is: "hey how doing check link: http://www.google.com , if dont link try link http://yahoo.com or try http://tinyurl.com/wp-tinyurl"

i make these links clickable in listview. not want use android:autolink="web" on textview object list item clickable , these can consume click event or cause confusion. im looking way scan through text , collect links , change them spanurl way text becomes clickable not textview. if makes difference here textview within row layout of listview 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 believe have handle programatically.

update: link provided here ended doing:

public class linkifier {   public textview setlinks(textview tv, string text) {     string[] linkpatterns = {             "([hh][tt][tt][pp][ss]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])",             "#[\\w]+", "@[\\w]+" };     (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 can create linkifier class , ran this: linkifier linkifier= new linkifier(); linkifier.setlinks(mytextview, "my message etc");

the textview has have text in , 2nd paramter matches on text looking for. if mytextview contained "hey how doing check link: http://www.google.com , if dont link try link http://yahoo.com or try http://tinyurl.com/wp-tinyurl" 2nd parameter put same string.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -