tags:

views:

487

answers:

2

Hi,

I want to add haptic feedback to my application buttons and control them programatically to show button state (enabled and disabled) The default haptic feedback setter works only for long press. How can i make it work for simple button clicks.

Amd is there a way to have haptic feedback on events like touch move??

A: 

View has a performHapticFeedback function, which should allow you to perform it whenever you want, i.e., on an OnClick listener.

Mayra
I added the following code in the onClick method of the onClickListener but it didn't workbtn_left .performHapticFeedback(HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);Im not really sure how this flag works
Funkyidol
Did you set haptic feedback to enabled? Per the documentation: http://developer.android.com/intl/fr/reference/android/view/View.html#performHapticFeedback(int) this is necessary to get any haptic feedback to play. I'm not sure about that constant, did you try using VIRTUAL_KEY?
Mayra
HapticFeedbackConstants.VIRTUAL_KEY is not available. Im currently using Android 1.5_r2And yes the haptic feedback is enabled for the buttons in the xml itself.Still no luck
Funkyidol
+2  A: 

Here is an answer, though it might not be the best implementation:

import android.view.View;
import android.os.Vibrator;

public class Main extends Activity implements OnClickListener
{
    private View myView;
    private Vibrator myVib;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);

        //myView can be any type of view, button, etc.
        myView = (View) this.findViewById(R.id.myView);
        myView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        myVib.vibrate(50);
        //add whatever you want after this
    }
}

Don't forget, you also need to add the "android.permission.VIBRATE" permission to the program's manifest. You can do so by adding the following to the "AndroidManifest.xml" file:

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

I hope that helps.

borg17of20