Touchevent Executing Both If And Else Part
Solution 1:
Its because OnTouchListener
calls continuously, so you need to check that user touches first time by MotionEvent.ACTION_DOWN
wv.setOnTouchListener(newOnTouchListener() {
publicbooleanonTouch(View v, MotionEvent event) {
if(event.getAction()== MotionEvent.ACTION_DOWN)
{
if(flag)
{
Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "if", 1000);
toast1.show();
upperdock.bringToFront();
tocparent.bringToFront();
tocbottom.bringToFront();
upperdock.setVisibility(RelativeLayout.VISIBLE);
tocparent.setVisibility(LinearLayout.VISIBLE);
tocbottom.setVisibility(LinearLayout.VISIBLE);
flag=false;
}
else
{
Toast toast2 = Toast.makeText(getBaseContext(), Boolean.toString(flag) + "else", 1000);
toast2.show();
wv.bringToFront();
upperdock.setVisibility(RelativeLayout.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
tocbottom.setVisibility(LinearLayout.INVISIBLE);
flag=true;
}
}
return flag;
}
});
Solution 2:
There are many action for touch event so please use it with if condition.
1.MotionEvent.ACTION_DOWN
2.MotionEvent.ACTION_MOVE
3.MotionEvent.ACTION_UP
.
.
.
etc
and always return false;
Update
if(event.getAction()== MotionEvent.ACTION_DOWN)
{
//when you touch on screen
}
elseif(event.getAction()== MotionEvent.ACTION_MOVE)
{
//when you move your finger on screen
}
elseif(event.getAction()== MotionEvent.ACTION_UP)
{
//when you release your finger from screen
}
Solution 3:
It only looks like both are being executed at the same time. In reality, they are executed separately, but because the touch event is being fired so rapidly, it appears that they're both executed at the same time. What you should be doing is detecting the type of the motion event first e.g., MotionEvent.ACTION_MOVE
or something. Otherwise, all motion events are handled by your listener code.
Of course the reason both if
and else
are called in your code is because you're setting the flag to true and false in each statement, so that the execution of the statements alternates.
Post a Comment for "Touchevent Executing Both If And Else Part"