Example usage for android.view GestureDetector GestureDetector

List of usage examples for android.view GestureDetector GestureDetector

Introduction

In this page you can find the example usage for android.view GestureDetector GestureDetector.

Prototype

public GestureDetector(Context context, OnGestureListener listener) 

Source Link

Document

Creates a GestureDetector with the supplied listener.

Usage

From source file:com.example.android.pdfrendererbasic.PdfRendererFragment.java

@Override
public void onStart() {
    super.onStart();
    try {//from   www . j av  a 2s.  co  m
        mPdfView.useBestQuality(true);
        PDFView.Configurator c = (firstTimeStart || currentFile == null ? mPdfView.fromAsset(FILENAME)
                : mPdfView.fromUri(currentFile));
        c.enableSwipe(false) // allows to block changing pages using swipe
                .swipeHorizontal(false).enableDoubletap(false).defaultPage(0).enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null).scrollHandle(null).enableAntialiasing(true) // improve rendering a little bit on low-res screens
                .load();
        gestureListener = new GestureListener(mPdfView, this);
        final GestureDetector gestureDetector = new GestureDetector(getActivity(), gestureListener);
        mPdfView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureDetector.onTouchEvent(event);
                return true;
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getActivity(), "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

From source file:org.easyaccess.phonedialer.CallingScreen.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calling);

    recipientTextView = (TextView) findViewById(R.id.recipientTextView);
    btnAnswerCall = (Button) findViewById(R.id.btnAnswerCall);
    btnRejectCall = (Button) findViewById(R.id.btnRejectCall);
    tv_dailer_called_timer = (TextView) findViewById(R.id.tv_dailer_called_timer);
    gestureDetector = new GestureDetector(getApplicationContext(), new GestureListener());

    mHandler = new Handler() {
        public void handleMessage(Message msg) {
            tv_dailer_called_timer.setText("" + time);
        }/*ww  w.j  a  v  a  2s .  c om*/
    };

    if (getIntent().getExtras() != null) {

        if (Utils.callingDetails != null && Utils.callingDetails.get("name") != null) {
            // Retrieve the name of the recipient and the type of the number from the Bundle
            String name = Utils.callingDetails.get("name");
            String typeOfNumber = Utils.callingDetails.get("type");

            if (getIntent().getExtras().getInt("type", -1) == Utils.OUTGOING) {
                // outgoing call
                callerDetails = getString(R.string.calling) + " " + name + ": " + typeOfNumber;
                hideAnswerButton();
            } else {
                // incoming call
                callerDetails = getString(R.string.call_from) + name + ": " + typeOfNumber;
                showAnswerAndRejectButtons();
            }

        } else if (Utils.callingDetails != null) {

            // Retrieve the name of the recipient and the type of the number from the Bundle
            if (getIntent().getExtras().getInt("type", -1) == Utils.OUTGOING) {
                // outgoing call
                callerDetails = getString(R.string.calling) + Utils.callingDetails.get("number");
                hideAnswerButton();
            } else {
                // incoming call
                callerDetails = getString(R.string.call_from) + Utils.callingDetails.get("number");
                showAnswerAndRejectButtons();
            }
        }

        if (Utils.callingDetails != null) {
            displayCall(callerDetails, Utils.callingDetails.get("number"));
        }

    } else {
        recipientTextView.setVisibility(View.VISIBLE);
        recipientTextView.setText(getString(R.string.error) + "!");
        recipientTextView.setContentDescription(getString(R.string.error));
    }

    btnAnswerCall.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            answerCall();
        }
    });

    btnRejectCall.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            rejectCall();

        }
    });

    this.bReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Utils.CALL_ENDED)) {
                // check if keyboard is connected but accessibility services
                // are disabled

                //call end stop timer
                if (myTimer != null) {
                    myTimer = null;
                }

                System.out.println("calling " + intent.getExtras().getString("message"));
                if (!Utils.isAccessibilityEnabled(getApplicationContext())
                        && getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS)
                    TTS.speak(intent.getExtras().getString("message"));
                Toast.makeText(getApplicationContext(), intent.getExtras().getString("message"),
                        Toast.LENGTH_SHORT).show();
                // check if there is any active call (no. of calls); if so,
                // activate the current call,
                if (Utils.off_hook == 1 || Utils.ringing == 1) {

                } else {
                    // All calls ended, finish activity
                    finish();
                    Utils.off_hook = 0;
                }

            }
        }
    };

}

From source file:com.perchtech.humraz.blind.OcrCaptureActivity.java

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.ocr_capture);
    Speech.getInstance().say("You Are Now In Character Recognition Mode.");
    mPreview = (CameraSourcePreview) findViewById(R.id.preview);
    mGraphicOverlay = (GraphicOverlay<OcrGraphic>) findViewById(R.id.graphicOverlay);

    boolean autoFocus = true;
    boolean useFlash = false;

    int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
    if (rc == PackageManager.PERMISSION_GRANTED) {
        createCameraSource(autoFocus, useFlash);
    } else {/* w  w  w  .  j  a  v  a  2 s  .co m*/
        requestCameraPermission();
    }

    gestureDetector = new GestureDetector(this, new CaptureGestureListener());
    scaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());

    Snackbar.make(mGraphicOverlay, "Tap to Speak. Pinch/Stretch to zoom", Snackbar.LENGTH_LONG).show();

    TextToSpeech.OnInitListener listener = new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(final int status) {
            if (status == TextToSpeech.SUCCESS) {
                Log.d("OnInitListener", "Text to speech engine started successfully.");
                tts.setLanguage(Locale.US);
            } else {
                Log.d("OnInitListener", "Error starting the text to speech engine.");
            }
        }
    };
    tts = new TextToSpeech(this.getApplicationContext(), listener);
}

From source file:com.eyetools.pediatrics.pediatricfixation.MainFixationFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_main_fixation, container, false);

    mainAct = (MainFixation) this.getActivity();
    /* load savedinstancestate */
    if (savedInstanceState != null) {
        // NTD/*from ww w.  j a v a  2 s .  c om*/
    }

    /* load preferences */
    prefManager = new PrefManager(getActivity());
    prefManager.loadSettings();
    mMajorViewType = prefManager.getMajorViewType();
    mViewIndex = prefManager.getViewIndex();
    mAltViewIndex = prefManager.getAltViewIndex();
    mSound = prefManager.getSoundSwitch();
    mPro = prefManager.getPro();
    mLicenseInfo = prefManager.getLicenseInfo();

    /* get UI elements */
    soundSwitch = (Switch) v.findViewById(R.id.sound_switch);
    soundSwitch.setChecked(mSound);
    soundSwitch.setOnCheckedChangeListener(this);
    nearButton = (ToggleButton) v.findViewById(R.id.near_button);
    nearButton.setChecked(mMajorViewType == NEAR);
    nearButton.setOnCheckedChangeListener(this);
    gifView = (WebView) v.findViewById(R.id.gifwebview);
    gesturePanel = (RelativeLayout) v.findViewById(R.id.touchpanel);

    mGestureDetector = new GestureDetector(mainAct, new SwipeListener());
    gesturePanel.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            mGestureDetector.onTouchEvent(motionEvent);
            return true;
        }
    });

    /* define animation modes */
    mViewTypes = new ArrayList<ArrayList>(MODE_COUNT);
    // add the large GIFs
    ArrayList<Anim> viewtype0 = new ArrayList<Anim>();
    viewtype0.add(new Anim("gif", "tunnel.gif", R.raw.sfx1));
    viewtype0.add(new Anim("gif", "color2.gif", R.raw.sfx2));
    viewtype0.add(new Anim("gif", "colors.gif", R.raw.sfx6));
    viewtype0.add(new Anim("gif", "colorsquares.gif", R.raw.sfx4));
    viewtype0.add(new Anim("gif", "pinkballs.gif", R.raw.sfx5));
    viewtype0.add(new Anim("gif", "swirls.gif", R.raw.sfx6));
    viewtype0.add(new Anim("gif", "weird.gif", R.raw.sfx7));
    mViewTypes.add(viewtype0);
    // add the small GIFs
    ArrayList<Anim> viewtype1 = new ArrayList<Anim>();
    viewtype1.add(new Anim("gif", "dinosaur.gif", R.raw.sfx6));
    viewtype1.add(new Anim("gif", "smallcartoon.gif", R.raw.sfx5));
    viewtype1.add(new Anim("gif", "smallkaleidoscope.gif", R.raw.sfx4));
    viewtype1.add(new Anim("gif", "smallredanimal.gif", R.raw.sfx2));
    mViewTypes.add(viewtype1);

    // show the last used animation
    ArrayList<Anim> lastViewType = mViewTypes.get(mMajorViewType);
    showAnim(lastViewType.get(mViewIndex).filename, mMajorViewType == NEAR);

    // show first time window
    if (prefManager.getFirstTime()) {
        mainAct.showAbout();
    }

    // initialize billing system
    String base64EncodedPublicKey;
    base64EncodedPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl6i3vQfVZpj/bqtl23AkUHQkatMEKkuHqKUqeaybnGPxYsdrKYtcCV0jzFyQoBPFXk4kqGIFg+3+jXnd3V05Z2f6Dt2ZTmKZKrzIhgFoyvR9yHp6CwekZmBr1aZIm1yPBIxNk4uPEz5wJjtqUW8J/fqm2L539ZFsDhizWptD7rNFUfTJ3Vtu00lNk1db2a6d6FzON1l90rZ1574tjP0vSWgK9u7SYiAagukfj0UGJ5zpXUvUHKTWFXWp/5okKlSGEjf52tVnH+Wsx/HjlwyK6H87K0/UqpPctTraSJesZBgY5uB+Qe5rKZsjq+6K48yAqZTev9UmyoxUOsiCxf5rKQIDAQAB";
    billingHelper = new IabHelper(mainAct, base64EncodedPublicKey);
    billingHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        @Override
        public void onIabSetupFinished(IabResult result) {
            if (!result.isSuccess()) {
                Log.d(TAG, "failed to set up InApp Billing");
            } else {
                Log.d(TAG, "successfully st up in app billing");
            }
        }
    });

    ArrayList<String> billingQueryList = new ArrayList<String>();
    billingQueryList.add("1001");

    billingHelper.queryInventoryAsync(true, billingQueryList, new IabHelper.QueryInventoryFinishedListener() {
        @Override
        public void onQueryInventoryFinished(IabResult result, Inventory inv) {

        }
    });

    return v;

}

From source file:com.minio.io.alice.MainActivity.java

public MainActivity() {
    gestureDetector = new GestureDetector(context, new GestureListener());
    if (XDebug.LOG)
        Log.i(MainActivity.TAG, "Instantiated new " + this.getClass());
}

From source file:zuo.biao.library.base.BaseActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    context = getActivity();//from   w  ww  . j av  a  2 s  .  co m
    isAlive = true;
    fragmentManager = getSupportFragmentManager();

    inflater = getLayoutInflater();

    gestureDetector = new GestureDetector(this, this);//??
    threadNameList = new ArrayList<String>();
}

From source file:com.numenta.htmit.mobile.notification.NotificationListActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gestureDetector = new GestureDetector(getApplicationContext(), this);
    setContentView(R.layout.activity_notification_list);
    setVisible(false);/*from   www  .j  av  a2 s.c  o  m*/

    listView = (ListView) findViewById(R.id.notification_list_view);
    dismissButton = (Button) findViewById(R.id.action_dismiss_all_notifications);
    closeButton = (Button) findViewById(R.id.action_close_notifications);
    noNotificationsText = (TextView) findViewById(R.id.no_notifications_text);

    // For the cursor adapter, specify which columns go into which views
    String[] fromColumns = { "timestamp", "description", "metric_id", "read" };
    int[] toViews = { R.id.notification_time, R.id.notification_description, R.id.notification_delete,
            R.id.notification_unread }; // The TextView in simple_list_item_1
    _database = HTMITApplication.getDatabase();
    adapter = new SimpleCursorAdapter(this, R.layout.fragment_notification_list_item, null, fromColumns,
            toViews, 0);

    new AsyncTask<Void, Void, Cursor>() {
        @Override
        protected Cursor doInBackground(Void... params) {
            unreadNotificationSize = _database.getUnreadNotificationCount();
            return _database.getNotificationCursor();
        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            setVisible(true);
            adapter.changeCursor(cursor);
            notificationSize = cursor.getCount();
            updateButtons();
        }
    }.execute();

    _notificationsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (adapter != null) {
                new AsyncTask<Void, Void, Cursor>() {
                    @Override
                    protected Cursor doInBackground(Void... params) {
                        if (isCancelled())
                            return null;
                        return _database.getNotificationCursor();
                    }

                    @Override
                    protected void onPostExecute(Cursor cursor) {
                        adapter.changeCursor(cursor);
                        updateButtons();
                    }
                }.execute();
            }
        }
    };

    adapter.setViewBinder(new ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            final int viewId = view.getId();

            switch (viewId) {
            case R.id.notification_time:
                // Converts the timestamp to a readable time.
                final int timeIndex = cursor.getColumnIndex("timestamp");
                Date date = new Date(cursor.getLong(timeIndex));
                ((TextView) view).setText(sdf.format(date));
                break;
            case R.id.notification_unread:
                // Hides notification icon if already read.
                if (cursor.getInt(cursor.getColumnIndex("read")) < 1) {
                    view.setVisibility(View.VISIBLE);
                } else {
                    view.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.notification_delete:
                // Adds click handler for notification deletions
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i(TAG, "{TAG:ANDROID.ACTION.NOTIFICATION.DELETE} Delete notification clicked");
                        View layout = (View) v.getParent();
                        int position = listView.getPositionForView(layout);
                        NotificationListActivity.this.removeNotificationAt(position);
                    }
                });
                break;
            default:
                return false;
            }
            return true;
        }
    });

    listView.setAdapter(adapter);

    // Clicks on the notifications themselves navigates to the detail view.
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
            Log.i(TAG,
                    "{TAG:ANDROID.ACTION.NOTIFICATION.SELECT} Notification navigation should occur here to notification "
                            + position);
            Cursor cursor = (Cursor) adapter.getItem(position);
            int localIdx = cursor.getColumnIndex("_id");
            final int localId = cursor.getInt(localIdx);

            new AsyncTask<Void, Void, Intent>() {
                @Override
                protected Intent doInBackground(Void... v) {
                    // Get the metric necessary for the new intent to view
                    // the metric detail
                    // page.
                    Notification notification = _database.getNotificationByLocalId(localId);
                    if (notification == null) {
                        // The notification or metric was deleted as the
                        // user view the list
                        return null;
                    }
                    Metric metric = _database.getMetric(notification.getMetricId());
                    if (metric == null) {
                        // the metric was deleted, so nowhere to go
                        _database.deleteNotification(localId);
                        return null;
                    }
                    Intent metricDetail = NotificationUtils
                            .createMetricDetailIntent(NotificationListActivity.this, notification);
                    // Mark the notification as read

                    if (!notification.isRead()) {
                        _database.markNotificationRead(localId);
                    }
                    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancelAll();

                    return metricDetail;
                }

                @Override
                protected void onPostExecute(Intent metricDetail) {
                    if (metricDetail == null) {
                        Toast.makeText(NotificationListActivity.this, R.string.notification_expired,
                                Toast.LENGTH_LONG).show();
                        NotificationListActivity.this.finish();
                        return;
                    }
                    // Show detail page
                    startActivity(metricDetail);
                    // Hide the 'unread' indication.
                    view.findViewById(R.id.notification_unread).setVisibility(View.INVISIBLE);
                }
            }.execute();

        }
    });

    // This catches "fling" events on items to delete notifications within
    // the list.
    // Defers touch events to a GestureDetector, which isolates fling events
    // from touch events.
    listView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });

    dismissButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i(TAG, "{TAG:ANDROID.ACTION.NOTIFICATION.DELETE_ALL}");
            deleteAllNotifications();
        }
    });

    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cancelNotifications();
        }
    });
}

From source file:com.groksolutions.grok.mobile.notification.NotificationListActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gestureDetector = new GestureDetector(getApplicationContext(), this);
    setContentView(R.layout.activity_notification_list);
    setVisible(false);/* www .  j  av a2s .  c  o m*/

    listView = (ListView) findViewById(R.id.notification_list_view);
    dismissButton = (Button) findViewById(R.id.action_dismiss_all_notifications);
    closeButton = (Button) findViewById(R.id.action_close_notifications);
    noNotificationsText = (TextView) findViewById(R.id.no_notifications_text);

    // For the cursor adapter, specify which columns go into which views
    String[] fromColumns = { "timestamp", "description", "metric_id", "read" };
    int[] toViews = { R.id.notification_time, R.id.notification_description, R.id.notification_delete,
            R.id.notification_unread }; // The TextView in simple_list_item_1
    grokDb = HTMITApplication.getDatabase();
    adapter = new SimpleCursorAdapter(this, R.layout.fragment_notification_list_item, null, fromColumns,
            toViews, 0);

    new AsyncTask<Void, Void, Cursor>() {
        @Override
        protected Cursor doInBackground(Void... params) {
            unreadNotificationSize = grokDb.getUnreadNotificationCount();
            return grokDb.getNotificationCursor();
        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            setVisible(true);
            adapter.changeCursor(cursor);
            notificationSize = cursor.getCount();
            updateButtons();
        }
    }.execute();

    _notificationsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (adapter != null) {
                new AsyncTask<Void, Void, Cursor>() {
                    @Override
                    protected Cursor doInBackground(Void... params) {
                        if (isCancelled())
                            return null;
                        return grokDb.getNotificationCursor();
                    }

                    @Override
                    protected void onPostExecute(Cursor cursor) {
                        adapter.changeCursor(cursor);
                        updateButtons();
                    }
                }.execute();
            }
        }
    };

    adapter.setViewBinder(new ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            final int viewId = view.getId();

            switch (viewId) {
            case R.id.notification_time:
                // Converts the timestamp to a readable time.
                final int timeIndex = cursor.getColumnIndex("timestamp");
                Date date = new Date(cursor.getLong(timeIndex));
                ((TextView) view).setText(sdf.format(date));
                break;
            case R.id.notification_unread:
                // Hides notification icon if already read.
                if (cursor.getInt(cursor.getColumnIndex("read")) < 1) {
                    view.setVisibility(View.VISIBLE);
                } else {
                    view.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.notification_delete:
                // Adds click handler for notification deletions
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i(TAG, "{TAG:ANDROID.ACTION.NOTIFICATION.DELETE} Delete notification clicked");
                        View layout = (View) v.getParent();
                        int position = listView.getPositionForView(layout);
                        NotificationListActivity.this.removeNotificationAt(position);
                    }
                });
                break;
            default:
                return false;
            }
            return true;
        }
    });

    listView.setAdapter(adapter);

    // Clicks on the notifications themselves navigates to the detail view.
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
            Log.i(TAG,
                    "{TAG:ANDROID.ACTION.NOTIFICATION.SELECT} Notification navigation should occur here to notification "
                            + position);
            Cursor cursor = (Cursor) adapter.getItem(position);
            int localIdx = cursor.getColumnIndex("_id");
            final int localId = cursor.getInt(localIdx);

            new AsyncTask<Void, Void, Intent>() {
                @Override
                protected Intent doInBackground(Void... v) {
                    // Get the metric necessary for the new intent to view
                    // the metric detail
                    // page.
                    Notification notification = grokDb.getNotificationByLocalId(localId);
                    if (notification == null) {
                        // The notification or metric was deleted as the
                        // user view the list
                        return null;
                    }
                    Metric metric = grokDb.getMetric(notification.getMetricId());
                    if (metric == null) {
                        // the metric was deleted, so nowhere to go
                        grokDb.deleteNotification(localId);
                        return null;
                    }
                    Intent metricDetail = NotificationUtils
                            .createMetricDetailIntent(NotificationListActivity.this, notification);
                    // Mark the notification as read

                    if (!notification.isRead()) {
                        grokDb.markNotificationRead(localId);
                    }
                    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancelAll();

                    return metricDetail;
                }

                @Override
                protected void onPostExecute(Intent metricDetail) {
                    if (metricDetail == null) {
                        Toast.makeText(NotificationListActivity.this, R.string.notification_expired,
                                Toast.LENGTH_LONG).show();
                        NotificationListActivity.this.finish();
                        return;
                    }
                    // Show detail page
                    startActivity(metricDetail);
                    // Hide the 'unread' indication.
                    view.findViewById(R.id.notification_unread).setVisibility(View.INVISIBLE);
                }
            }.execute();

        }
    });

    // This catches "fling" events on items to delete notifications within
    // the list.
    // Defers touch events to a GestureDetector, which isolates fling events
    // from touch events.
    listView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });

    dismissButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i(TAG, "{TAG:ANDROID.ACTION.NOTIFICATION.DELETE_ALL}");
            deleteAllNotifications();
        }
    });

    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cancelNotifications();
        }
    });
}

From source file:com.YOMPsolutions.YOMP.mobile.notification.NotificationListActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gestureDetector = new GestureDetector(getApplicationContext(), this);
    setContentView(R.layout.activity_notification_list);
    setVisible(false);//from  w w  w  .  j a v a  2s  .  c o  m

    listView = (ListView) findViewById(R.id.notification_list_view);
    dismissButton = (Button) findViewById(R.id.action_dismiss_all_notifications);
    closeButton = (Button) findViewById(R.id.action_close_notifications);
    noNotificationsText = (TextView) findViewById(R.id.no_notifications_text);

    // For the cursor adapter, specify which columns go into which views
    String[] fromColumns = { "timestamp", "description", "metric_id", "read" };
    int[] toViews = { R.id.notification_time, R.id.notification_description, R.id.notification_delete,
            R.id.notification_unread }; // The TextView in simple_list_item_1
    YOMPDb = YOMPApplication.getDatabase();
    adapter = new SimpleCursorAdapter(this, R.layout.fragment_notification_list_item, null, fromColumns,
            toViews, 0);

    new AsyncTask<Void, Void, Cursor>() {
        @Override
        protected Cursor doInBackground(Void... params) {
            unreadNotificationSize = YOMPDb.getUnreadNotificationCount();
            return YOMPDb.getNotificationCursor();
        }

        @Override
        protected void onPostExecute(Cursor cursor) {
            setVisible(true);
            adapter.changeCursor(cursor);
            notificationSize = cursor.getCount();
            updateButtons();
        }
    }.execute();

    _notificationsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (adapter != null) {
                new AsyncTask<Void, Void, Cursor>() {
                    @Override
                    protected Cursor doInBackground(Void... params) {
                        if (isCancelled())
                            return null;
                        return YOMPDb.getNotificationCursor();
                    }

                    @Override
                    protected void onPostExecute(Cursor cursor) {
                        adapter.changeCursor(cursor);
                        updateButtons();
                    }
                }.execute();
            }
        }
    };

    adapter.setViewBinder(new ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            final int viewId = view.getId();

            switch (viewId) {
            case R.id.notification_time:
                // Converts the timestamp to a readable time.
                final int timeIndex = cursor.getColumnIndex("timestamp");
                Date date = new Date(cursor.getLong(timeIndex));
                ((TextView) view).setText(sdf.format(date));
                break;
            case R.id.notification_unread:
                // Hides notification icon if already read.
                if (cursor.getInt(cursor.getColumnIndex("read")) < 1) {
                    view.setVisibility(View.VISIBLE);
                } else {
                    view.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.notification_delete:
                // Adds click handler for notification deletions
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i(TAG, "{TAG:ANDROID.ACTION.NOTIFICATION.DELETE} Delete notification clicked");
                        View layout = (View) v.getParent();
                        int position = listView.getPositionForView(layout);
                        NotificationListActivity.this.removeNotificationAt(position);
                    }
                });
                break;
            default:
                return false;
            }
            return true;
        }
    });

    listView.setAdapter(adapter);

    // Clicks on the notifications themselves navigates to the detail view.
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
            Log.i(TAG,
                    "{TAG:ANDROID.ACTION.NOTIFICATION.SELECT} Notification navigation should occur here to notification "
                            + position);
            Cursor cursor = (Cursor) adapter.getItem(position);
            int localIdx = cursor.getColumnIndex("_id");
            final int localId = cursor.getInt(localIdx);

            new AsyncTask<Void, Void, Intent>() {
                @Override
                protected Intent doInBackground(Void... v) {
                    // Get the metric necessary for the new intent to view
                    // the metric detail
                    // page.
                    Notification notification = YOMPDb.getNotificationByLocalId(localId);
                    if (notification == null) {
                        // The notification or metric was deleted as the
                        // user view the list
                        return null;
                    }
                    Metric metric = YOMPDb.getMetric(notification.getMetricId());
                    if (metric == null) {
                        // the metric was deleted, so nowhere to go
                        YOMPDb.deleteNotification(localId);
                        return null;
                    }
                    Intent metricDetail = NotificationUtils
                            .createMetricDetailIntent(NotificationListActivity.this, notification);
                    // Mark the notification as read

                    if (!notification.isRead()) {
                        YOMPDb.markNotificationRead(localId);
                    }
                    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancelAll();

                    return metricDetail;
                }

                @Override
                protected void onPostExecute(Intent metricDetail) {
                    if (metricDetail == null) {
                        Toast.makeText(NotificationListActivity.this, R.string.notification_expired,
                                Toast.LENGTH_LONG).show();
                        NotificationListActivity.this.finish();
                        return;
                    }
                    // Show detail page
                    startActivity(metricDetail);
                    // Hide the 'unread' indication.
                    view.findViewById(R.id.notification_unread).setVisibility(View.INVISIBLE);
                }
            }.execute();

        }
    });

    // This catches "fling" events on items to delete notifications within
    // the list.
    // Defers touch events to a GestureDetector, which isolates fling events
    // from touch events.
    listView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });

    dismissButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.i(TAG, "{TAG:ANDROID.ACTION.NOTIFICATION.DELETE_ALL}");
            deleteAllNotifications();
        }
    });

    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cancelNotifications();
        }
    });
}