0

What i want : I want to create a edit text that will vibrate if given input is empty or invalid.

Example : In login screen the password edit text is empty or invalid, than edit text will vibrate at the same time my android device need to vibrate for some time how to create that it?
thanks in advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Agilanbu
  • 2,747
  • 2
  • 28
  • 33

2 Answers2

1

Alright here is what you need..

Vibrate Animation

put these two xml files inside res/anim folder

vibrate.xml

<?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_5" />

cycle_5.xml

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

Vibrating Phone

use these method to vibrate your phone for 500 milliseconds

public void shakeItBaby() {
    int DURATION = 500; // you can change this according to your need 
    if (Build.VERSION.SDK_INT >= 26) {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(DURATION, VibrationEffect.DEFAULT_AMPLITUDE));
    } else {
        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(DURATION);
    }
}

and don't forget to put permission in menifest

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

Putting all together

now use them both wile validating your EditText

Animation vibrate = AnimationUtils.loadAnimation(this, R.anim.vibrate);

if (paytm_amt.getText().toString().trim().isEmpty()) {
    paytm_num.setError("Please Enter PayTM Number");
    paytm_num.startAnimation(vibrate);
    shakeItBaby();
}  else {
    // do something
}

Happy Coding..

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

Components

 EditText mPassword = findViewById(R.id.edtPassword);
 Button mLogin = findViewById(R.id.btnLogin);

Android Device Vibration

 private void AndroidDeviceVibrate() { // Android Device Vibration
    Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); // Vibrate for 500 milliseconds only
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
    } else {
        v.vibrate(500); // deprecated in API 26
    }
}

Edit text vibrate Animation

 public TranslateAnimation VibrateError() { // Edit text vibrate Animation
    TranslateAnimation vibrate = new TranslateAnimation(0, 10, 0, 0);
    vibrate.setDuration(600);
    vibrate.setInterpolator(new CycleInterpolator(8));
    return vibrate;
}

Finally,

mLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String strPass = mPassword.getText().toString();
            if (TextUtils.isEmpty(strPass)) {
                AndroidDeviceVibrate();  // Android Device Vibrate
                mPassword.startAnimation(VibrateError()); // Edit text vibrate Animation
            }
        }
    });
Agilanbu
  • 2,747
  • 2
  • 28
  • 33