0

I need to launch add event when user keep during 2 seconds on listview. After that vibrate device and show dialog asking "Add to favorite?".

I've trying whit this.

lv.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.AXIS_PRESSURE){

            long eventDuration = 
                    android.os.SystemClock.elapsedRealtime() 
                    - event.getDownTime();

        //Put up the Yes/No message box


        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

        alert
        .setTitle("Service")
        .setMessage("Add to favorite?")
        //.setIcon(R.drawable.chile1)
        .setPositiveButton("Si", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {                    
                 Intent intent = new Intent(Activity.class);
                    startActivity(intent);

            }

        })
        .setNegativeButton("No", null)                      //Do nothing on no
        .show();

    }
        return false;   
    }

the code above show 5 time the dialog.

Slinker
  • 379
  • 1
  • 4
  • 14
  • Consider using a thread? – Nabin May 29 '15 at 01:59
  • Long click, or click twice? It's not clear from your question. For a long click, this answer might help: http://stackoverflow.com/questions/8846707/how-to-implement-a-long-click-listener-on-a-listview/8846824#8846824 – Daniel Nugent May 29 '15 at 02:02

3 Answers3

1

You need to use OnItemLongClickListener(), like this:

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            alert.setTitle("Service")
                    .setMessage("Add to favorite?")
                    //.setIcon(R.drawable.chile1)
                    .setPositiveButton("Si", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Activity.class);
                            startActivity(intent);
                        }
                    })
                    .setNegativeButton("No", null) //Do nothing on no
                    .show();
            return true;
        });
hungryghost
  • 9,463
  • 3
  • 23
  • 36
0

First of all you need to allow Vibration on the Manifest file:

<uses-permission android:name="android.permission.VIBRATE"/>

Then for long pressing you can use mListView.setOnItemLongClickListener() like here:

list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> p, View v, int i, long id) {
        // Do Stuff
        return false;
    }
});

Then on Do Stuff you can call the Vibrator and then open the Dialog asking to add to favorite. Bellow is how you call the Vibrator:

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(200); // 200 miliseconds
felippe
  • 493
  • 6
  • 7
0

you can check code bellow :

private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(final View view,
                           final MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                handler.postDelayed(mLongPressed,
                                    1000);
                //This is where my code for movement is initialized to get original location.
                break;
            case MotionEvent.ACTION_UP:
                handler.removeCallbacks(mLongPressed);

                break;
            case MotionEvent.ACTION_MOVE:
                handler.removeCallbacks(mLongPressed);
                //Code for movement here. This may include using a window manager to update the view
                break;
        }
        return true;        }
};



//Put this into the class
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
    public void run() {
        //TODO :show dialog when hold 1s.
        // can you set time show dialog handler.postDelayed(mLongPressed, 1000);

    }
};

set OnTouch: lv.setOnTouchListener(onTouchListener);

QuestionAndroid
  • 881
  • 1
  • 8
  • 25