简体   繁体   中英

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

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)
}

You should add this listener to EditText for your desired validation,

editText.addTextChangedListener(new  TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
            // Write your logic here
                    if(condition satisfy)
                    // call vibrate();  
            }
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }
            public void afterTextChanged(Editable s) {

            }
        });



        public void vibrate()
        {
                Vibrator vibrate= (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE) ;
                           vibrate.vibrate(50);
        }

here is code for Kotlin

private fun vibrator() {
    vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator?
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        vibrator!!.vibrate(
            VibrationEffect.createOneShot(
                500,
                VibrationEffect.DEFAULT_AMPLITUDE
            )
        )
    } else {
        //deprecated in API 26
        vibrator!!.vibrate(500)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM