Detect when the touch event is over in Android - java

How can I detect when the touch event is over in Android,in Webviews specifically.
pseudocode:
if (touch event is over) then
do something
end
Thanks!

The MotionEvent object passed to the onTouchEvent(...) method has a getAction() method, you can use it to determine if the event is over. eg:
webViews.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//pointer down.
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_OUTSIDE:
//event has finished, pointer is up or event was canceled or the pointer is outside of the view's bounds
break;
}
return false;
}
}
Also, if you want to consume the event (stop it from propagating) just make the onTouch(...) method return true instead of false.

Related

OnLongCLickListener is not working as I need

I have a TextView, with a onLongClickListener and OnClick event, on holding TextView, its color changes to red, and on releasing, its color is supposed to change to white.
Problem:
When I hold the TextView and move my finger outside of it while holding, and then leave my finger, its color does not change to white.
XML
<TextView
android:layout_width="match_parent"
android:text="hello"
android:textColor="#ffff"
android:id="#+id/timer"
android:layout_height="wrap_content"
/>
Java
final TextView t1 = (TextView) findViewById(R.id.timer);
t1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setTextColor(Color.WHITE);
}
});
t1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
t1.setTextColor(Color.RED);
return false;
}
});
View.OnClickListener - Interface definition for a callback to be invoked when a view is clicked.
View.OnLongClickListener - Interface definition for a callback to be invoked when a view has been clicked AND held.
So what you are saying is 100% true.It should be red because its being been clicked and held as the way you did.
But when i hold the text view and move my finger outside the text view
while holding , and then leave my finger , it not changes its color to
white
You have given color white to text view when it gets only clicked !! If you want to get that white like you said(when clicked and held), you need to set the white color in OnLongClickListener
To the point if you want to detect your views touch and release and change colors related to that then you need to use OnTouchListener instead of clickListeners
View.OnTouchListener - Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view
t1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
t1.setTextColor(Color.RED); // pressed state
break;
case MotionEvent.ACTION_UP:
t1.setTextColor(Color.WHITE); // Released state
break;
}
return true;
}
});
Assign a onTouch listener and look for MotionEvent.ACTION_DOWN and MotionEvent.ACTION_MOVE:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Construct a rect of the view's bounds
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
// User moved outside bounds
t1.setTextColor(Color.WHITE);
}
}
return false;
}
Use OnTouchListener that way you can register touch down and up events. MotionEvent case MotionEvent.ACTION_DOWN: will set the color to red when the user touches down on your TextView, and case MotionEvent.ACTION_UP: will set the color to white when the user lifts their finger off the TextView.
final TextView t1 = (TextView) findViewById(R.id.timer);
t1.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
t1.setTextColor(Color.RED);
break;
case MotionEvent.ACTION_UP:
t1.setTextColor(Color.WHITE);
break;
}
return true;
}
});

Issue with changing img resource android

I am having this problem when I try to change the source/resource of a image(a button) when is pressed and released.
Btn.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
/*if(event.getAction() == MotionEvent.ACTION_DOWN) {
ImageBtn.setImageResource(R.drawable.btn1);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
ImageBtn.setImageResource(R.drawable.btn0);
}*/
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
ImageBtn.setBackgroundResource(R.drawable.btn1);
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
ImageBtn.setBackgroundResource(R.drawable.btn1);
return true; // if you want to handle the touch event
}
return false;
}
});
This is the code, I tried what is in the comment too, but no result. I do have to mention that the button changes its resource just when touching it, but after releasing it, it doesn't change.
The button listener is on a relative layout(also defined as a relative layout in the xml code), in java however is defined as a button.
The image of the button doesn't change from when you touch it to when you release it because you are setting the same background resource.
Both cases contain:
ImageBtn.setBackgroundResource(R.drawable.btn1);
Set one of them to a different image.

Double Tap in android View

In a view I want to detect both single tap and double tap. It works fine with me. I can detect both single and double tap. Please find my code
#Override
public boolean onTouch(View view, MotionEvent me) {
// No dragging during animation at the moment.
// TODO: Stop animation on touch event and return to drag mode.
boolean result = true;
result = gestureDetector.onTouchEvent(me);//return the double tap events
if(result)
return false;
switch (me.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
private String fileName;
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
// event when double tap occurs
#Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
if(player!=null && player.isPlaying())
{
player.stop();
}
else
{
setFileName(x);
player = new AudioPlayer(fileName);
player.play();
}
return true;
}
}
I am doing some code in Action_UP event. So even when I do a double tap, for the first tap the ACTION_UP event is called and the corresponding code is executed. I want to prevent this from happening. How can I do this. I just want to make sure that the ACTION_UP is called only when the user is done with his gesture. Not immediately after his partial gesture . In this case its being called on finger up of first tap.
How can I make this work in android?
Thanks
According to gesture listener documentation, you need to return true for onSingleTapConfirmed & onDoubleTap. Otherwise the event is not consumed and gestureDetector.onTouchEvent(me); returns false.

Need boolean value isTouched()

I need to detect WHEN the screen is touched. The onTouchEvent method only detects when the finger is moving. I need method which returns boolean value true, when the finger touches screen and returns false, when its not.
Here's a very basic implementation of the onTouch method that modifies a boolean value to know if a the screen is touched. You may need to tweak it to suit your specific needs (and maybe handle multi touch)
private boolean mIsScreenTouched;
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN :
case MotionEvent.ACTION_MOVE :
mIsScreenTouched = true;
break;
case MotionEvent.ACTION_CANCEL :
case MotionEvent.ACTION_UP :
mIsScreenTouched = false;
break;
}
return true;
}
#Override
public boolean dispatchTouchEvent(MotionEvent e) {
// TODO Auto-generated method stub
super.dispatchTouchEvent(e);
if(btn.onTouchEvent(e)){
return btn.onTouchEvent(e);
}else{
return false;
}
}

issue with if/else and switch statements

the following code is to handle ACTION_DOWN and ACTION_UP events for a button named clash. the idea is that once if/else determines that the onTouch event was caused by clash and the switch statement then determines what to do based on the action. i don't know if the problem is that the switch statements are not returning true and that may be related to the problem. when i add a return, eclipse says that the code is unreachable which i don't understand. i was under the impression that you can't break out of a switch without break.
what's actually happening is that the first sound will loop but the code never seems to detect the action up when the button is released and so the sound plays forever. any help would be appreciated.
public boolean onTouch(View v, MotionEvent event) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
if (v.getId() == R.id.clash){
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
mp.pause();
break;
}
}
return true;
}
});
//Add the following line in the code before your setOnTouchListener()
MediaPlayer mp;
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.clash){
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mp = MediaPlayer.create(getBaseContext(), R.raw.clash);
mp.setLooping(true);
mp.start();
break;
case MotionEvent.ACTION_UP:
if(mp != null)
{
mp.pause();
mp.release();
}
break;
}
}
}
// I'm assuming this was from the onTouchListener()? -> });
Just a thought.

Categories

Resources