Grokking Android

Getting Down to the Nitty Gritty of Android Development

Displaying Error Hints in Forms on Android

By

Displaying error messages in forms on Android is really simple. Most widgets have a setError(CharSequence text) method that can be used to indicate problems.

Say you have a form where you have to enter a product. Now while entering more data into the form the app might check the product number and some backend service might say, that the number is invalid. In this case a quick feedback would be nice. That's exactly what can be done with this method:

Standard error hint in Android
Standard error hint in Android

The setError() method is only available for widgets that extend TextView. But gladly all relevant Widgets for error messages do inherit from TextView: EditText, AutoCompleteTextView, CheckBox and RadioButton (well, the hierarchy is weird, but never mind).

But there is one problem with using this approach. The message text is only visible when the view has the focus. Otherwise Android only displays the red marker. Try setting the focus on a touch screen with a radio button or a check box. Good luck to you. If you have a keyboard, trackball or so on, this is okay. But for most devices the necessity to focus is bound to fail. I would have preferred if the exclamation mark would have been clickable. Of course you can add a click listener and focus yourself, but this kind of defeats the purpose of this approach.

Problem with error hints in Android
Problem with error hints in Android

On the other hand this might not be that bad, since check boxes should only rarely fail a check and radio buttons in my opinion should never be allowed to do so. For check boxes a shopping app comes to mind where you must tick a check box to agree to some conditions before being able to proceed.

Then there is also the oddity that for EditText fields the error hint disappears as soon as the field has lost its focus. In my opinion the hint should only disappear when the value has changed.

Even despite the mentioned problems you should add a setError() method whenever you create you own widgets. And you should not stray too far from the standard design and usage patterns for displaying problems in your apps. As always I would recommend to stay close to the platform but adopt to your needs.

Wolfram Rittmeyer lives in Germany and has been developing with Java for many years.

He has been interested in Android for quite a while and has been blogging about all kind of topics around Android.

You can find him on Google+ and Twitter.