Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vibration of Edittext in android

i want to create a edit text that will vibrate if given input is invalid. for example edit text for number if number is wrong like it contain 9 digits than edit text will became clear and will vibrate for some time how to create that? thanks in advance

like image 336
Sahil Patel Avatar asked Mar 14 '13 05:03

Sahil Patel


4 Answers

Create anim folder in resources and then create file named shake.xml and paste the below code

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0" android:toXDelta="10" android:duration="1000"
    android:interpolator="@anim/cycle_7" />

and another file cycle_7.xml

<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />

and then in your java file

if(invalid)//check your input
{
   Animation shake = AnimationUtils.loadAnimation(Login.this, R.anim.shake);
   editText.startAnimation(shake);
}
like image 100
Priya Avatar answered Nov 14 '22 13:11

Priya


If anyone is looking for a method to do what @Priya suggested programatically, then you can try this.

public TranslateAnimation shakeError() {
        TranslateAnimation shake = new TranslateAnimation(0, 10, 0, 0);
        shake.setDuration(500);
        shake.setInterpolator(new CycleInterpolator(7));
        return shake;
}

And then:

myEditText.startAnimation(shakeError());
like image 45
vishal-wadhwa Avatar answered Nov 14 '22 13:11

vishal-wadhwa


For vibrate use the following code.

Vibrator vibe = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

Then, in the OnTextChanged Listener method use the following code.

vibe.vibrate(50); // 50 is time in ms

And don't forget you need to add the permission to the manifest (after the </application> tag):

<uses-permission android:name="android.permission.VIBRATE" />
like image 5
Hardik Joshi Avatar answered Nov 14 '22 12:11

Hardik Joshi


Create shake.xml under anim folder

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="-10"
        android:toXDelta="10"
        android:repeatCount="5"
        android:repeatMode="reverse"
        android:interpolator="@android:anim/linear_interpolator"
        android:duration="70" />
</set>

After this add animation for button. I wrote this code in Kotlin for simplicity.

button.setOnClickListener {
      button.startAnimation(AnimationUtils.loadAnimation(context, R.anim.shake)
}
like image 4
JEGADEESAN S Avatar answered Nov 14 '22 13:11

JEGADEESAN S