Reputation: 1963
In the code below the problem i m facing is that in onTouchListener method both if and else part is getting executed each time. Anyone have idea why is it happening like that? and aim of my project is on touch event on anywhere in on the webview the dock layout must appear on another touch it must disappear.Can anyone help regarding this?
Thanks in advance ..
public class MathiasdockActivity extends Activity {
/** Called when the activity is first created. */
RelativeLayout upperdock,parent;
LinearLayout tocbottom,tocparent;
boolean flag=true;
WebView wv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
parent=(RelativeLayout) findViewById(R.id.rl1);
upperdock=(RelativeLayout) findViewById(R.id.upperdock);
tocparent=(LinearLayout) findViewById(R.id.tocparent);
tocbottom=(LinearLayout) findViewById(R.id.linearLayout3);
upperdock.setVisibility(RelativeLayout.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
tocbottom.setVisibility(LinearLayout.INVISIBLE);
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/hotspot/07-03.html");
wv.getSettings().setJavaScriptEnabled(true);
wv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
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;
}
});
}
}
Upvotes: 3
Views: 946
Reputation: 9182
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.
Upvotes: 1
Reputation: 42016
Its because OnTouchListener
calls continuously, so you need to check that user touches first time by MotionEvent.ACTION_DOWN
wv.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(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;
}
});
Upvotes: 1
Reputation: 33248
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
}
else if(event.getAction()== MotionEvent.ACTION_MOVE)
{
//when you move your finger on screen
}
else if(event.getAction()== MotionEvent.ACTION_UP)
{
//when you release your finger from screen
}
Upvotes: 1