Izak Izak - 14 days ago 6
Android Question

Android - Double tap WebView detect

I am creating an app with a WebView and a menu on top of the page. I made that menu is invisible and would only appear when a button is clicked. That worked fine but that button couldn't fit anywhere nicely so instead of clicking a button I made it so that the menu would appear when you double tap on the screen.
So here's the problem. Double tap only works on empty space, but not on WebView. How would I make it work, so it would appear/disappear whenever I double tap the screen?

public class MainActivity extends Activity implements OnGestureListener{

private WebView webView;
LinearLayout toolbar;
private GestureDetector gd;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_main);

webView =((WebView)findViewById(R.id.webView1));
webView.setWebViewClient(new WebViewClient());
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

webView.setVerticalScrollBarEnabled(true);

webView.loadUrl("URL_HERE");

gd = new GestureDetector(this);

gd.setOnDoubleTapListener(new OnDoubleTapListener()
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
LinearLayout toolbar= (LinearLayout)findViewById(R.id.toolbar);

if(toolbar.getVisibility() == View.GONE){
toolbar.setVisibility(View.VISIBLE);

}
else {
toolbar.setVisibility(View.GONE);

}
return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e)
{
return false;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e)
{
return false;
}
});


}

@Override
public boolean onTouchEvent(MotionEvent event)
{
return gd.onTouchEvent(event);//return the double tap events
}

@Override
public boolean onDown(MotionEvent e)
{
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
return false;
}

@Override
public void onLongPress(MotionEvent e)
{
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}

@Override
public void onShowPress(MotionEvent e)
{
}

@Override
public boolean onSingleTapUp(MotionEvent e)
{
return false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}


Any help will be appreciated.

Answer

Did you try adding this ?

webView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gd.onTouchEvent(event);
    }
});
Comments