Skip to content

Instantly share code, notes, and snippets.

@naxrohan
Created July 20, 2019 11:39
Show Gist options
  • Save naxrohan/ebf350c1c444225669fc5e42e32c0df1 to your computer and use it in GitHub Desktop.
Save naxrohan/ebf350c1c444225669fc5e42e32c0df1 to your computer and use it in GitHub Desktop.
GestureListener for Webview to call a PopupWindow.
package com.example.myapplication;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.Toast;
public class GestureListener extends GestureDetector.SimpleOnGestureListener {
public static final String TAG = "GestureListener";
public static Context context;
public View view;
public PopupWindow popmenu;
public GestureListener(Context context, View view) {
super();
this.view = view;
this.context = context;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i(TAG, "--Long Press" + getTouchType(e));
// get click & adjust offsets
int x_click = Math.round(e.getX()) - 100;
int y_click = Math.round(e.getY()) + 280;
//todo : change menu location as per click location
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View customView = layoutInflater.inflate(R.layout.popup,null);
ImageButton bookmark = (ImageButton) customView.findViewById(R.id.bookmark);
ImageButton underline = (ImageButton) customView.findViewById(R.id.underline);
ImageButton highlight = (ImageButton) customView.findViewById(R.id.highlight);
popmenu = new PopupWindow(customView, 500, ActionBar.LayoutParams.WRAP_CONTENT);
popmenu.showAtLocation(this.view, Gravity.NO_GRAVITY, x_click, y_click);
bookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context.getApplicationContext(),"Bookmark added",Toast.LENGTH_SHORT).show();
popmenu.dismiss();
}
});
underline.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WebView xview = (WebView)view;
xview.loadUrl("javascript:alert(showAndroidToast('underline'))");
view = xview;
popmenu.dismiss();
}
});
highlight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WebView xview = (WebView)view;
xview.loadUrl("javascript:alert(showAndroidToast('highlight'))");
view = xview;
popmenu.dismiss();
}
});
popmenu.setOutsideTouchable(true);
popmenu.setFocusable(true);
// Show anchored to button
popmenu.setBackgroundDrawable(new BitmapDrawable());
}
// BEGIN_INCLUDE(init_gestureListener)
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.i(TAG, "--Single Tap Up" + getTouchType(e));
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.i(TAG, "--Scroll" + getTouchType(e1));
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
Log.i(TAG, "--Fling" + getTouchType(e1));
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// User performed a down event, and hasn't moved yet.
Log.i(TAG, "--Show Press" + getTouchType(e));
}
@Override
public boolean onDown(MotionEvent e) {
// "Down" event - User touched the screen.
Log.i(TAG, "--Down" + getTouchType(e));
if(popmenu != null){
popmenu.dismiss();
}
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// User tapped the screen twice.
Log.i(TAG, "--Double tap" + getTouchType(e));
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// Since double-tap is actually several events which are considered one aggregate
// gesture, there's a separate callback for an individual event within the doubletap
// occurring. This occurs for down, up, and move.
Log.i(TAG, "--Event within double tap" + getTouchType(e));
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// A confirmed single-tap event has occurred. Only called when the detector has
// determined that the first tap stands alone, and is not part of a double tap.
Log.i(TAG, "--Single tap confirmed" + getTouchType(e));
return false;
}
// END_INCLUDE(init_gestureListener)
/**
* Returns a human-readable string describing the type of touch that triggered a MotionEvent.
*/
private static String getTouchType(MotionEvent e){
String touchTypeDescription = " ";
int touchType = e.getToolType(0);
switch (touchType) {
case MotionEvent.TOOL_TYPE_FINGER:
touchTypeDescription += "(finger)";
break;
case MotionEvent.TOOL_TYPE_STYLUS:
touchTypeDescription += "(stylus, ";
//Get some additional information about the stylus touch
float stylusPressure = e.getPressure();
touchTypeDescription += "pressure: " + stylusPressure;
if(Build.VERSION.SDK_INT >= 21) {
touchTypeDescription += ", buttons pressed: " + getButtonsPressed(e);
}
touchTypeDescription += ")";
break;
case MotionEvent.TOOL_TYPE_ERASER:
touchTypeDescription += "(eraser)";
break;
case MotionEvent.TOOL_TYPE_MOUSE:
touchTypeDescription += "(mouse)";
break;
default:
touchTypeDescription += "(unknown tool)";
break;
}
return touchTypeDescription;
}
/**
* Returns a human-readable string listing all the stylus buttons that were pressed when the
* input MotionEvent occurred.
*/
@TargetApi(21)
private static String getButtonsPressed(MotionEvent e){
String buttons = "";
if(e.isButtonPressed(MotionEvent.BUTTON_PRIMARY)){
buttons += " primary";
}
if(e.isButtonPressed(MotionEvent.BUTTON_SECONDARY)){
buttons += " secondary";
}
if(e.isButtonPressed(MotionEvent.BUTTON_TERTIARY)){
buttons += " tertiary";
}
if(e.isButtonPressed(MotionEvent.BUTTON_BACK)){
buttons += " back";
}
if(e.isButtonPressed(MotionEvent.BUTTON_FORWARD)){
buttons += " forward";
}
if (buttons.equals("")){
buttons = "none";
}
return buttons;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment