0

How can I change the visibility of an EditText view once the user stops typing or clicks done? I tried to do it this but I got an error message.

    package com.example.android.monopolycredit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

public class LoginLobby extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_lobby);

        final EditText usernameEditText = findViewById(R.id.usernameField);
        usernameEditText.setOnEditorActionListener(
                new EditText.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                        if (actionId == EditorInfo.IME_ACTION_SEARCH ||
                                actionId == EditorInfo.IME_ACTION_DONE ||
                                event.getAction() == KeyEvent.ACTION_DOWN &&
                                        event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                            if (!event.isShiftPressed()) {
                                // the user is done typing.
                                usernameEditText.setVisibility(View.GONE);
                                return true; // consume.
                            }
                        }
                        return false; // pass on to other listeners.
                    }
                });
    }

}

Any help would be appreciated. I'm new to android development. Thanks in advance. :)

jmecs
  • 143
  • 2
  • 13
  • 1
    What do you define as finished typing? The `EditText` losing focus or some time passed between the user's last char input or one character entered? – Veneet Reddy Dec 31 '17 at 18:24
  • As soon as the user presses enter. I guess losing focus isn't the best way to do this. – jmecs Dec 31 '17 at 18:26
  • What is the error message you get ?? – Misha Akopov Dec 31 '17 at 18:44
  • The problem right now is that when I press enter the app just stops working. I'm obviously doing something wrong but I don't get what. I made the usernameEditText variable final, too. – jmecs Dec 31 '17 at 19:37
  • you can use `addTextChangedListener` for this. – Aditya Dec 31 '17 at 19:58

1 Answers1

0

How can I change the visibility of an EditText view once the user stops typing...

There is no such event which tells you that user has stopped typing. Even if user stops typing you still have a focus in that EditText field which means that he can proceed typing. Therefore in this case, you can't determine if he really stopped entering text.

or clicks done?

That's already possible. In order to listen to keyboard changes such as done action just add actionId == EditorInfo.IME_ACTION_DONE to your existing code:

EditText usernameEditText = findViewById(R.id.usernameField);
usernameEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
        if (actionId == EditorInfo.IME_ACTION_DONE && !event.isShiftPressed()) {
            // the user is done typing.
            usernameEditText.setVisibility(View.GONE);
            return true;
        }
        return false;
    }
});
flyingAssistant
  • 872
  • 7
  • 19
  • When I run this code my application crashes once I click done. – jmecs Dec 31 '17 at 20:47
  • Can you please share stack trace? – flyingAssistant Dec 31 '17 at 20:54
  • Probably because I declared usernameEditText as final, but I end up changing it later on in the code. I don't know how to get around it because I get this error * Variable usernameEditText is accessed from within the class, needs to be declared final* when I don't make the usernameEditText variable as final. – jmecs Dec 31 '17 at 21:03
  • It seems you need to do a nullpointer check: event != null && event.isShiftPressed(). This SO post may help: https://stackoverflow.com/questions/11301061/null-keyevent-and-actionid-0-in-oneditoraction-jelly-bean-nexus-7 – flyingAssistant Dec 31 '17 at 21:41
  • It's no problem to change the properties of an object, a final variable 'points' to. You just cannot assign a different object to the final variable. – Ridcully Dec 31 '17 at 23:45