• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Showing EditText-hint for limited time

FSchmidt

Lurker
Hello guys,

I want to change the hint of an editText-inputfield for two seconds, if the user inserts wrong data.

I tried to do something like this:

Java:
new Thread() {
                    @Override
                    public void run() {
                        try {
                            input1.setHint(R.string.emptyEditTextFieldHint);
                            input2.setHint(R.string.emptyEditTextFieldHint);
                            this.sleep(2000);
                        } catch (InterruptedException e) {
                        } finally {
                            input1.setHint(R.string.input1Hint);
                            input2.setHint(R.string.input2Hint);
                            return;
                        }
                    }
                }.start();


But the program crashes (I guess it's because of the InterruptionException / .sleep()). How can I fix it? I know there is an easy way, but I can't figure it out write now.

Kind regards and thanks for your responses.
F.S.
 
What's the stack trace?

At a guess I'll say that the problem is caused by you trying to update a UI component, from a non-UI thread.

If you want to do input validation, then why not just use a Toast message?
 
This is the (important part of) stack trace:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

I've already got a Toast, but the hint change would look awesome ;)

How can I fix it?

Kind regards!
F.S.
 
I've tried my code surrounded with

this.runOnUiThread(new Runnable() {
.....
});

But it sadly didn't work. There is no error, but the hint does not show up...
 
EditText already has a way to handle incorrect user input...
Java:
input1.setError("Nope! Wrong input!");

Another thing to note. The hint is only displayed in an empty EditText. So if the user's incorrect input is still there then your hint will not show up. You would have to clear the edittext first and then show your hint.

The approach you're trying to take is not user friendly...
Say I enter the wrong input. I wouldn't want the app to delete it just to show me a hint. What if I just need to change one character? I wouldn't want to type it all over again.

setError() is the way to go on this.
 
Hello,
the hint would just show up, if both inputfields were completly empty.

I now fixed the problem with a toast and .setError on both inputfields.

Thanks for your help.


Edit: Is there a way to just display the error symbol without error message?
 
Is there a way to just display the error symbol without error message?

As far as I know you cannot remove the error message from the error icon. So Ideally it would be best to remove the toast as to not be redundant with error messages.
 
Back
Top Bottom