Make clickable textview in android -
by way have showed ids db.
textview setnote = (textview) findviewbyid(r.id.idnotes); setnote.settext(""); //empty current text //db connection databasehandler db = new databasehandler(this); list<contact> contacts = db.getall(); (contact cn : contacts) { integer id = cn.getid(); setnote.append(id.tostring()+"\n"); }
result
12
35
42
now want make these ids clickable such when user clicks on each id, open separate activity corresponding id. how can this?
if want make whole textview clickable can add onclicklistener others have mentioned.
however, if want make each of id's you're printing clickable separately (ie. each 1 goes somewhere else), you'll have create add own span
class.
here's example implementation. first define own span extending clickablespan
:
static class myclickablespan extends clickablespan { onclicklistener mlistener; public myclickablespan(onclicklistener listener) { mlistener = listener; } @override public void onclick(view widget) { mlistener.onclick(widget); } }
next, want create spannablestring
each of id's print:
for (contact cn : contacts) { string id = cn.getid().tostring(); spannablestring myspan = new spannablestring(id+"\n") myspan.setspan(new myclickablespan(new onclicklistener() { public void onclick(view v) { //do whatever want when clicked here! <---- } }), 0, id.length(), spanned.span_exclusive_exclusive); setnote.append(myspan); }
finally, enable clicking, need add linkmovementmethod
textview
follows:
// put @ end after finishing for-loop setnote.setmovementmethod(linkmovementmethod.getinstance());
this allow make each of id's clickable each 1 goes separate activity
if that's want
Comments
Post a Comment