Android: Only make certain part of custom view be clickable -
i have custom view, assume looks this:
i custom view respond onclicks, catch respond clicks on red portion/circle. not whole view.
is possible make make text above , grey portion not clickable?
thank you.
in custom view, handle clicks overriding ontouchevent
method of android's view class. first check location user has clicked within circle. give feedback on motionevent.action_down
event let user know have clicked, such highlight circle. on motionevent.action_up
can call onclick method.
@override public boolean ontouchevent(motionevent event) { boolean istouchincircle = checktouchincircle(event.getx(), event.gety()); switch (event.getaction()) { case motionevent.action_down: if (istouchincircle) { circlecolor = highlightcolor; invalidate(); } break; case motionevent.action_move: if (istouchincircle) { circlecolor = highlightcolor; } else { circlecolor = normalcolor } invalidate(); break; case motionevent.action_up: if (istouchincircle) { onclickcircle(); } break; } return true; } // circle click zone approximated square private boolean checktouchincircle(float touchx, float touchy) { if (touchx < circlecenterx + circleradius && touchx > circlecenterx - circleradius && touchy < circlecentery + circleradius && touchy > circlecentery - circleradius) { return true; } else { return false; } }
Comments
Post a Comment