Skip to main content

Shake a view(edit text) on incorrect input at OTP,passwords,etc (Android)

Hello devs!

This is another short tutorial to show how to shake an Edit text view if the input is incorrect.
Note: The tutorial assumes that you have the basic knowledge about android development.



It involves the following steps:

1. Create the Edit text and cast it inside the onCreate.

EditText edittext=findViewById(R.id.edit_text);

Recommended:https://github.com/GoodieBag/Pinview

Optional- Put the edit text inside a dialog to take an input from the dialog.

2. Write an animation XML.
Here is an example: (shake.xml)
Courtesy:https://gist.github.com/simon-heinen/9795036


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

Store this inside the anim folder in res.

3.Write the onClick function for a button and inside that, whenever a condition is true, use the following lines:

Animation example= AnimationUtils.loadAnimation(context, R.anim.shake);
view.startAnimation(example);

In our case the condition would be something like,

if(edittext.getText().toString().equals("password"))
{
//action
}
else{
Animation example= AnimationUtils.loadAnimation(context, R.anim.shake);
edittext.startAnimation(example);

Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show()
}


You can use different animations according to your need.
That's all folks! Hope it helps..


Comments

Popular posts from this blog

Create drop-down menu with image icons in Android

Hello devs! In this tutorial, we will be creating a drop-down menu on clicking a button in Android studio. Here is an image of how our menu is going to look: On clicking the hamburger icon, a vertical menu will appear with image icons of your choice. Above image is just to get an idea of what we are going to develop. Let's get started. Step 1: Open the layout file where you want the menu. Add the hamburger icon on an image button. <ImageButton android :layout_width= "50dp" android :layout_height= "50dp" android :src= "@drawable/ham" android :id= "@+id/ham" android :background= "#00000000" />  Note : #00000000 specifies 'transparent' background. Step 2: Add a vertical linear layout for the menu. Inside that, add nested linear layouts for horizontal rows. <LinearLayout android :layout_width= "wrap_content" android :layout_height= "wrap_content&quo

Fix for eclipse oxygen icon not displaying in ubuntu.

There is a bug in the latest version of eclipse IDE. The icon is not displayed. If you are facing the same problem then I might have a fix for you. There are various reasons why this may occur. One of the problems is caused because icon of eclipse is not present in pixmaps folder. To add it simply copy the icon of eclipse into /use/share/pixmaps folder. Note: You need to have root access to do that.  Also, make sure the name of the icon is eclipse.xpm  For this, use the terminal to fire the command: sudo cp /eclipse_path/icon.xpm /usr/share/pixmaps/eclipse.xpm Here replace the eclipse_path with your eclipse folder path. E.g. /home/leena/eclipse/java-oxygen/eclipse/icon.xpm