android - TextView onTouchEvent within customview -
i have custom view, , within custom view create 4 text views. want each of these text views respond on touch events. however, textviews not respond on touch event. can however, make ontouch listener entire custom view. however, not want whole view have ontouch event because want textviews have created dragged , dropped. tried going route of registering x , y coordinates of ontouch event , assuming if ontouch event within bounds of textview, change coordinates of textview, overly complicated because if 1 textview got dragged coordinates of one, ontouch event "pick up" other textview, i'd moving 2 text views, not want. so, sum up, want know if it's possible set in ontouch listener textview inside customview, , if possible, why not working:
mscale.mpositive.get(0).setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { mscale.mcurrentxpos[0] = event.getx(); mscale.mcurrentypos[0] = event.gety(); mscale.mdrag = true; return true; } });
this same code works custom view, not specific textviews inside customview.
here custom view code:
public class scale extends view {
public scale(context context, attributeset attrs) { super(context, attrs); mcontext = this.getcontext(); h = new handler(); mcalendardbhelper=new calendardbadapter(mcontext); mcalendardbhelper.open(); cursor thoughts = mcalendardbhelper.fetchthoughts(); //create string array of negative thoughts db while (thoughts.movetonext()) { if (thoughts.getstring(thoughts.getcolumnindexorthrow(calendardbadapter.column_name_thought)).length() > 0 && thoughts.getstring(thoughts.getcolumnindexorthrow(calendardbadapter.column_name_thought)).charat(0) == '-') { negative_thoughts.add(thoughts.getstring(thoughts.getcolumnindexorthrow(calendardbadapter.column_name_thought))); } } thoughts.close(); array_size = negative_thoughts.size(); mbag =bitmapfactory.decoderesource(getresources(), r.drawable.bag); mgreenbag = bitmapfactory.decoderesource(getresources(), r.drawable.green_bag); (int = 0; < 72; ++) { try { mscale[i] = bitmapfactory.decodestream(context.getassets().open("scale_"+i+".gif")); } catch (ioexception e) { } } } private runnable r= new runnable() { @override public void run() { invalidate(); } }; protected void ondraw (canvas canvas) { if (first == true) { width = this.getwidth(); height = this.getheight(); mscale[i] = bitmap.createscaledbitmap(mscale[i], (int) (width * 1.5), height, true); mbag = bitmap.createscaledbitmap(mbag, width/2, height/2, true); negative = new textview(mcontext); word = negative_thoughts.get((int) (math.random() * array_size)); negative.settext(word); negative.layout(0, 0, width/3, height/4); negative.setgravity(gravity.center); negative.settextsize(15); negative.settextcolor(color.black); negative.settypeface(typeface.default_bold); negative.setshadowlayer(5, 2, 2, color.white); negative.setdrawingcacheenabled(true); negative.setbackgroundresource(r.drawable.graycloud); positive_paint.setcolor(color.parsecolor("#ff4444")); positive_paint.setshadowlayer(5, 2, 2, color.yellow); positive_paint.settypeface(typeface.default_bold); positive_paint.settextsize(25); mcurrentxpos[0] = (width/2); mcurrentypos[0] = height/4; mcurrentxpos[1] = (width/2) + (width/8); mcurrentypos[1] = height/6; mcurrentxpos[2] = width/2; mcurrentypos[2] = height/12; mcurrentxpos[3] = (width/2) + (width/8); mcurrentypos[3] = height/18; mmovexpos[0] = ((width/2) - width)/frame_rate; mmoveypos[0] = ((height/4) - (height + (height/4)))/frame_rate; mmovexpos[1] = (((width/2) + (width/8)) - width)/ frame_rate; mmoveypos[1] = ((height/6) - (height + (height/4)))/frame_rate; mmovexpos[2] = ((width/2) - width)/ frame_rate; mmoveypos[2] = ((height/12) - (height + (height/4)))/frame_rate; mmovexpos[3] = (((width/2) + (width/8)) - width)/ frame_rate; mmoveypos[3] = ((height/18) - (height + (height/4)))/frame_rate; mmovebyxpos[0] = -(width/2)/ frame_rate; mmovebyypos[0] = -(height/4)/frame_rate; mmovebyxpos[1] = ((width - (width/3)) - (width/2 + (width/8)))/ frame_rate; mmovebyypos[1] = -(height/6)/frame_rate; mmovebyxpos[2] = - (width/2)/ frame_rate; mmovebyypos[2] = ((height) - (height/12))/frame_rate; mmovebyxpos[3] = ((width - (width/3)) - (width/2 + (width/8)))/ frame_rate; mmovebyypos[3] = ((height) - (height/18))/frame_rate; currentx = width; currenty = height + height/4; first = false; } if (game_over == false) { canvas.drawbitmap(mscale[i], 0 - (width/4), 0, null); canvas.drawbitmap(negative.getdrawingcache(),(int) (width/12), (int) (height - (height)/2.5) - (j), null); } else { canvas.drawbitmap(mbag, width/4, height/4, null); } if (mmovescale == true) { i++; j+=3; scaleit(canvas, i); if (i == 21 || == 37 || == 53 || == 71) { mmovescale = false; } } if (tracker > 0) { if (tracker == 1) { if (currentx > width/2 && currenty > height/4 && sstop == false) { currentx += mmovexpos[0]; currenty += mmoveypos[0]; canvas.drawbitmap(mpositive.get(tracker -1 ).getdrawingcache(), currentx, currenty, null); } else { if (sstop == false) { mmovescale = true; sstop = true; currentx = width; currenty = height + height/4; draw_em++; } } } if (tracker == 2) { if (currentx > width/2 + (width/8) && currenty > (height/6) && sstop == false) { currentx += mmovexpos[1]; currenty += mmoveypos[1]; canvas.drawbitmap(mpositive.get(tracker -1 ).getdrawingcache(), currentx, currenty, null); } else { if (sstop == false) { mmovescale = true; sstop = true; currentx = width; currenty = height + height/4; draw_em++; } } } if (tracker == 3) { if (currentx > width/2 && currenty > height/12 && sstop == false) { currentx += mmovexpos[2]; currenty += mmoveypos[2]; canvas.drawbitmap(mpositive.get(tracker -1 ).getdrawingcache(), currentx, currenty, null); } else { if (sstop == false) { mmovescale = true; sstop = true; currentx = width; currenty = height + height/4; draw_em++; } } } if (tracker == 4) { if (currentx > width/2 + (width/8) && currenty > (height/18) && sstop == false) { currentx += mmovexpos[3]; currenty += mmoveypos[3]; canvas.drawbitmap(mpositive.get(tracker -1 ).getdrawingcache(), currentx, currenty, null); } else { if (sstop == false) { mmovescale = true; sstop = true; game_over = true; currentx = width; currenty = height + height/4; draw_em++; } } } if (draw_em > 0 && game_over == false) { (int = 0; < draw_em; ++) { if (i == 0) { canvas.drawbitmap(mpositive.get(i).getdrawingcache(), width/2, height/4 + j, null); } if (i == 1) { canvas.drawbitmap(mpositive.get(i).getdrawingcache(), width/2 + (width/8), height/6 + j, null); } if (i == 2) { canvas.drawbitmap(mpositive.get(i).getdrawingcache(), width/2, height/12 + j, null); } if (i == 3) { canvas.drawbitmap(mpositive.get(i).getdrawingcache(), width/2 + (width/8), height/18 + j, null); } } } else if (game_over == true) { (int = 0; < draw_em; i++) { if (i == 0 && mcurrentxpos[0] > 0 && mcurrentypos[0] > 0 && mdrag == false) { mcurrentxpos[0] += mmovebyxpos[0]; mcurrentypos[0] += mmovebyypos[0]; canvas.drawbitmap(mpositive.get(i).getdrawingcache(), mcurrentxpos[0], mcurrentypos[0], null); } else if (i == 0 && mcurrentxpos[0] <= 0 || mcurrentypos[0] <= 0 && mdrag == false) { canvas.drawbitmap(mpositive.get(0).getdrawingcache(), 0, 0, null); } else if (i == 0 && mdrag == true) { canvas.drawbitmap(mpositive.get(0).getdrawingcache(), mcurrentxpos[0], mcurrentypos[0], null); } if (i == 1 && mcurrentxpos[1] < (width - (mpositive.get(i).getwidth()/2)) && mcurrentypos[1] > mpositive.get(i).getheight()/2) { mcurrentxpos[1] += mmovebyxpos[1]; mcurrentypos[1] += mmovebyypos[1]; canvas.drawbitmap(mpositive.get(i).getdrawingcache(), mcurrentxpos[1], mcurrentypos[1], null); } else if (i == 1 && mcurrentxpos[1] >= (width - (mpositive.get(i).getwidth()/2)) || mcurrentypos[1] <= mpositive.get(i).getheight()/2) { canvas.drawbitmap(mpositive.get(1).getdrawingcache(), width - (width/3), 0, null); } if (i == 2 && mcurrentxpos[2] > (mpositive.get(i).getwidth()/2) && mcurrentypos[2] < (height - mpositive.get(i).getheight()/2)) { mcurrentxpos[2] += mmovebyxpos[2]; mcurrentypos[2] += mmovebyypos[2]; canvas.drawbitmap(mpositive.get(i).getdrawingcache(), mcurrentxpos[2], mcurrentypos[2], null); } else if (i == 2 && mcurrentxpos[2] <= (mpositive.get(i).getwidth()/2) || mcurrentypos[2] >= (height - mpositive.get(i).getheight()/2)) { canvas.drawbitmap(mpositive.get(2).getdrawingcache(), 0, height - (height/4), null); } if (i == 3 && mcurrentxpos[3] < (width - (mpositive.get(i).getwidth()/2)) && mcurrentypos[3] < (height - mpositive.get(i).getheight()/2)) { mcurrentxpos[3] += mmovebyxpos[3]; mcurrentypos[3] += mmovebyypos[3]; canvas.drawbitmap(mpositive.get(i).getdrawingcache(), mcurrentxpos[3], mcurrentypos[3], null); } else if (i == 3 && mcurrentxpos[3] >= (width - (mpositive.get(i).getwidth()/2)) || mcurrentypos[3] >= (height - mpositive.get(i).getheight()/2)) { canvas.drawbitmap(mpositive.get(3).getdrawingcache(), width - (width/3), height - (height/4), null); } } } } h.postdelayed(r, frame_rate); } protected void moveit(canvas canvas, int movex,int movey, int i) { if (i == 0) { canvas.drawbitmap(mpositive.get(i).getdrawingcache(), movex, movey, null); } if (i == 1) { canvas.drawbitmap(mpositive.get(i).getdrawingcache(), movex, movey, null); } if (i == 2) { canvas.drawbitmap(mpositive.get(i).getdrawingcache(), movex, movey, null); } if (i == 3) { canvas.drawbitmap(mpositive.get(i).getdrawingcache(), movex, movey, null); } } protected void moveem(canvas canvas, int[]mmoveposx, int[] mmoveposy) { (int = 0; < 4; i++) { } } protected void scaleit(canvas canvas, int i) { mscale[i] = bitmap.createscaledbitmap(mscale[i], (int) (width * 1.5), height, true); mscale[i-1].recycle(); } }
and here activity:
public class scaleview extends activity { context mcontext; scale mscale; edittext positive_thought; button fire; textview pos; private static set<string> mpositivewords; private static set<string> mnegativewords; int count; private pattern four_letter_words = pattern.compile("not|cant|cnt|can't"); string inputline; private string[] inputtokens; button question; button skip; public static boolean populatepositivewords(context context) { mnegativewords = new hashset<string>(); try { bufferedreader reader = new bufferedreader(new inputstreamreader(context.getassets().open("negative_words.txt"))); string line = reader.readline(); while (line != null) { mnegativewords.add(line.tolowercase(locale.us)); line = reader.readline(); } reader.close(); } catch (ioexception exception) { return false; } return true; //todo list of negative words } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.getactionbar().hide(); mcontext = this; populatepositivewords(mcontext); setcontentview(r.layout.activity_scale); mscale = (scale) findviewbyid(r.id.anim_view); mscale.setclickable(true); positive_thought = (edittext) findviewbyid(r.id.thoughts); fire = (button) findviewbyid(r.id.scale_it); skip = (button) findviewbyid(r.id.skip); question = (button) findviewbyid(r.id.question); inputfilter[] filterarray = new inputfilter[1]; filterarray[0] = new inputfilter.lengthfilter(60); positive_thought.setfilters(filterarray); fire.setonclicklistener(new onclicklistener() { @override public void onclick(view view) { //if button clicked invalidate ondraw method , pass in text of positive word inputline = positive_thought.gettext().tostring(); inputtokens = inputline.split(" "); if (inputline.isempty()) { toast.maketext(mcontext, "you have write something!", toast.length_short).show(); return; } if (inputtokens.length < 3) { toast.maketext(mcontext, "at least 3 words required.", toast.length_short).show(); return; } if (four_letter_words.matcher(inputline).find() == true) { toast.maketext(mcontext, "make affirmative statement!", toast.length_short).show(); return; } boolean matchestoken = false; (int = 0; < inputtokens.length; i++) { string token = inputtokens[i]; if (mnegativewords.contains(token.tolowercase(locale.us))) { matchestoken = true; break; } } if (matchestoken == true) { toast.maketext(mcontext, "use positive words!", toast.length_short).show(); return; } else { inputmethodmanager imm = (inputmethodmanager)mcontext.getsystemservice( context.input_method_service); imm.hidesoftinputfromwindow(positive_thought.getwindowtoken(), 0); pos = new textview (mcontext); pos.layout(0, 0, mscale.width/3, mscale.height/4); pos.setgravity(gravity.center); pos.settextsize(15); pos.settextcolor(color.red); pos.settypeface(typeface.default_bold); pos.setshadowlayer(5, 2, 2, color.yellow); pos.settext(positive_thought.gettext().tostring()); pos.setdrawingcacheenabled(true); pos.setbackgroundresource(r.drawable.whitecloud); pos.setclickable(true); mscale.mpositive.add(pos); mscale.scale_it = true; count++; mscale.sstop = false; mscale.tracker = count; if (count == 4) { ((relativelayout)question.getparent()).removeview(question); ((relativelayout)skip.getparent()).removeview(skip); mscale.mpositive.get(0).setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { mscale.mcurrentxpos[0] = event.getx(); mscale.mcurrentypos[0] = event.gety(); mscale.mdrag = true; return true; } }); } } positive_thought.settext(null); } }); }
}
the reason textview cannot receive touch events textview drawn on canvas bitmap, not view. excerpt code shown below illustrates this.
protected void ondraw(canvas canvs) { .... negative = new textview(mcontext); ... canvas.drawbitmap(negative.getdrawingcache(), ...)
to deliver touch events textview, scale class should extend not view viewgroup , textview needs added subview scale class using viewgroup.addview() or addviewinlayout(). not simple task implement viewgroup subclass. may have implement onintercepttouchevent(motionevent) depending on needs.
android's source code of help.
Comments
Post a Comment