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
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);
}
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());
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" />
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With