JohnNick
JohnNick

Reputation: 1766

How to determine long press listener on image view?

I am developing an android application. In my application,I have one image view. I want to determine long press listener on image view,when I long press on image i want to vibrate device.How is possible? Thanks all

Upvotes: 7

Views: 16475

Answers (3)

Jomon George
Jomon George

Reputation: 73

You have to set clickable true on the view. try setLongClickable(true)

ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setLongClickable(true);
final Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
imageView.setOnLongClickListener(new OnLongClickListener() {

     @Override
     public boolean onLongClick(View v) {
         vibrator.vibrate(100);
         return true;
     }
 });

Upvotes: 1

malik M
malik M

Reputation: 333

you can try this

ImageView iv = (ImageView) findViewById(R.id.ImageView);
iv.setOnLongClickListener(vlong);

private View.OnLongClickListener vLong = new View.OnLongClickListener() {
    public boolean onLongClick(View view) {
        // do any thing
        return true;
    }
};

Upvotes: 2

Nikolay Ivanov
Nikolay Ivanov

Reputation: 8935

You can try to do it this way:

    ImageView imageView = (ImageView) findViewById(R.id.ImageView);
    final Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
    imageView.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            vibrator.vibrate(100);
            return true;
        }
    });

Upvotes: 17

Related Questions