How to make your users aware of incorrect input in Android

Did you ever enter a wrong password and got an AlertDialog telling you that you entered something wrong?
You probably did and you probably also noticed that this AlertDialog most likely takes one more click to continue, a click that could be saved. One way to avoid the AlertDialog are Toasts. Here are two nice but rarely used other ways to tell your users that they should enter something different.

Setup:


Let’s assume we have an EditText which we use in our UI.

EditText mEditText ;
mEditText = (EditText ) findViewById(R.id.myEditText );

Furthermore we have a method showError() which we call when the EditText contains invalid data.

  • Shake:

A nice way to show the user that, for example, an entered password was incorrect is to shake the EditText. Please note that I took this code from the official ApiDemos and modified it slightly.

First, we have to define our shake animation. Go to your res folder, create the subfolder anim and place a file shake.xml in it. In this file, create a translation like this:

<translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0%" android:toXDelta="5%" android:duration="1000" android:interpolator="@anim/cycle_7" />
TranslateAnimation let us move views on the x or y axis of our screen. Since we want to shake it from left to right, we only apply the translation on the x axis. We move it from zero percent of the view’s width to five percent of the view’s width and let the translation last one second (1000 ms). Furthermore, we use the interpolator cylce_7 which is placed in anim/cycle_7.xml and looks like the following:

<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:cycles="7" />


It’s a simple CycleInterpolator. This kind of interpolators express the number of repetitions an animation should do. In our case, we repeat the animation seven times.
Now we only need to apply our shake animation to our EditText every time something incorrect is entered. We can do it like this:
private void showError() {
      Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
      mEditText.startAnimation(shake);
}
That’s it. Super easy, super smooth integration into the UI.

  • setError()

This is my personal favourite. It is the setError()-method which comes out of the box with the EditText view. When calling this method, the right hand compound drawable of the will be set to the error icon. When the EditText also has focus, the text you gave to setError() will be shown in a popup. If you don’t like the default error icon, you can also use setError(CharSequence error, Drawable icon) to set your own icon. The routine to show errors can then look like this:
private void showError() {    mEditText.setError("Password and username didn't match");}
Which will result in errors shown like this:
setError() on ICS
Figure: EditText setError();

Which looks good, catches the user’s attention and doesn’t need any extra clicks to disappear.

Conclusion

Showing errors without interrupting the user flow can be accomplished easily on the Android platform  For even more attention by the user, the two methods mentioned can also be combined.
Please feel free to share your methods of showing error messages in the comments.

Robot Programming in Java

Robot class:

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.


Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.

More about Robot class Click Here:

Example of Robot class:

I have created simple example for Robot class using KeyBoard Events:

Just Follow this Step:
  • Copy/Paste whole code in Notepad or any Editor.
  • Save with named "JavaRobotExample.java"
  • Run it
  • You may ask for Editor name to run in your computer for e.g. notepad, wordpad, winword etc. whatever you can write in Run window.
  • press Enter, now don't touch keyboard or mouse and just wait for while Running... Typing... Saving...
Have fun ;) 


import java.awt.AWTException;
import java.awt.Robot;
//import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Scanner;

public class JavaRobotExample
{
Robot robot = new Robot();
Scanner scanner;
public static void main(String[] args) throws AWTException
{
new JavaRobotExample();
}

public JavaRobotExample() throws AWTException
{

scanner = new Scanner(System.in);

String get;

System.out.print("\nEnter Program Name to Run In Your Computer... :- ");
get = scanner.next();

robot.setAutoDelay(50);
robot.setAutoWaitForIdle(true);

robot.delay(100);
robot.keyPress(KeyEvent.VK_WINDOWS);
robot.delay(500);
type("R");
robot.delay(500);
robot.keyRelease(KeyEvent.VK_WINDOWS);

robot.delay(500);
type(get);
robot.delay(1000);
type(KeyEvent.VK_ENTER);

robot.delay(5000);

type("Hello to everyone...!!");

type(KeyEvent.VK_ENTER);
robot.delay(50);
type(KeyEvent.VK_ENTER);

robot.delay(100);
type("This is a Robot Class Presentation in JAVA...!!!");
type(KeyEvent.VK_ENTER);
type(KeyEvent.VK_ENTER);

robot.delay(100);
type("Created By - Pratik Butani(M.C.A.)");
type(KeyEvent.VK_ENTER);
type(KeyEvent.VK_ENTER);
robot.delay(100);

type("- Android Developer at Kevalam Software");
type(KeyEvent.VK_ENTER);
type(KeyEvent.VK_ENTER);

robot.delay(100);
type("Contact No.- 88 66 22 45 46");
type(KeyEvent.VK_ENTER);
type(KeyEvent.VK_ENTER);
type("Thank You..!!!");

robot.delay(500);
robot.keyPress(KeyEvent.VK_CONTROL);

type("s");
robot.keyRelease(KeyEvent.VK_CONTROL);

robot.delay(1000);
type("pratik");
robot.delay(500);
type(KeyEvent.VK_ENTER);

robot.delay(1000);
type("y");        
robot.delay(1000);

robot.delay(1000);
type(KeyEvent.VK_ALT);
robot.delay(1000);
type("f");
robot.delay(1000);
type("x");

type("n");
System.exit(0);
}

private void type(int i)
{
robot.delay(50);
robot.keyPress(i);
robot.keyRelease(i);
}

private void type(String s)
{
byte[] bytes = s.getBytes();
for (byte b : bytes)
{
int code = b;
// keycode only handles [A-Z] (which is ASCII decimal [65-90])
if (code > 96 && code < 123) code = code - 32;
robot.delay(40);
robot.keyPress(code);
robot.keyRelease(code);
}
}
}

Have Fun :D

Thank you...