This has to be a very simple fix, but I can’t seem to figure it out. I have got my program to change the background color to change onClick, and onTouch with ACTION_DOWN and ACTION_UP. But I need it to change the color when touching the screen and going in and out of the TextView. I need it to function like a mouseOver/mouseOut event. Is there a way to do this on android? Or am I stuck with onTouch, where the action has to start from within the TextView?
Right now I set the onTouchListener on the TextView itself. Should I set it somewhere else and then check if the x and y are within the Textview? Or is there another event listener I should be using? I am new to Android, any help would be appreciated. Thanks.
Mike
I would implement the
OnTouchListeneron your application and then on theonTouchmethod, keep checking if the current position of the touch event is within the bounds of the bounding box of the view. If it is, apply the new background and if it isn’t apply the original one.Since all the views implement the
setBackgroundColorI didn’t do any casting toTextViewbut the example should suffice, at least as a starting point to develop your application further.The full code for this is the following:
As for the user interface for the previous code, it’s just a linear layout with an ID
contand a view (aTextViewin your case) with an IDtarget. The rest is totally by default so there is no point in me pasting it here. Note I only tested this on an emulator and ymmv when trying it on real devices, but as far as I can think of, it should be fine.Relevant documentation: