Showing posts with label press. Show all posts

Vibration adds a nice feel to your device, it makes the user feel part of your app and awaken. I am going to show you how to make your phone vibrate when you long press a button.





1. import the vibrate class

import android.os.Vibrator;

2. next, this will be the code that will listen for a long press, and make your phone vibrate.

button.setOnLongClickListener(new View.OnLongClickListener(){
    

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


Note: this will make your phone vibrate for 100 milliseconds, hence 1000ms is 1s

enjoy this nice feature.












How to make your device Vibrate onLong Click


Sometimes you may need to have a click and a long click (press) on the same View, here is how you can avoid triggering both event.





//button view 
button.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
       //your code here
   }
});


button.setOnLongClickListener(new View.OnLongClickListener() {
@Override public boolean onLongClick(View v) { //your code here return true; } });


By setting onLongClick return to true, you are telling the compiler that you have handled onLongClick event, no need to trigger the onClick event.


How To separate a LongClick from a Click event

- Copyright © Mdunge Apps - Blogger Templates - Powered by Blogger - Designed by Johanes Djogan -