Disabling and re-enabling an EditText field: Cursor and keyboard do not reappear after re-enabling it

Viewed 71

I am setting up a log in activity, with username and password. I want to disable the user name and password edit text fields whilst the app is trying to log the user on to the system.

    signInButton.setOnClickListener(view -> {

    valueAnimator.start();

    loginView.setEnabled(false);
    loginView.setFocusable(false);
    loginView.clearFocus();
    passwordView.setEnabled(false);
    passwordView.setFocusable(false);
    passwordView.clearFocus();

I want to then re-enable/activate the edit text fields if the sign in fails or is unsuccessful. I've followed the instructions on a few similar questions, but whenever I try and allow the fields to be editable again, the cursor and keyboard don't appear again. I can long click on the fields, and the cursor will appear, but won't allow any editing or change, without exiting and going back into app.

                passwordView.clearFocus();
                loginView.clearFocus();

                loginView.setEnabled(true);
               // loginView.setFocusable(true);
                loginView.setClickable(true);
                loginView.isFocusableInTouchMode();

                passwordView.setEnabled(true);
                // passwordView.setFocusable(true);
                passwordView.setClickable(true);
                passwordView.isFocusableInTouchMode();

                loginView.setCursorVisible(true);
                passwordView.setCursorVisible(true);
                loginView.requestFocus();

<EditText
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/prompt_login"
        android:focusable="true"/>

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/prompt_password"
        android:focusable="true"
        android:inputType="textPassword"
        android:visibility="gone" />

Currently using Android Studio 3.1.4, with

compileSdkVersion 28
defaultConfig {
    applicationId "co.***.app"
    minSdkVersion 21
    targetSdkVersion 27
2 Answers

You can use setEnabled(false) for disable the editText field and setEnabled(true)for enable the editText field.

If sign in fails or is unsuccessful you can again enable the edit text field, it depends on your response.

No need to use other attribute or method you can use only setEnabled(false)/setEnabled(true).

If you use this code when you press the singInButton:

 Button singInButton = findViewById(R.id.button);
        singInButton.setOnClickListener(view -> {

            loginView.setEnabled(false);
            loginView.clearFocus();
            passwordView.setEnabled(false);
            passwordView.clearFocus();


         });

And you do this when the error happens:

public void onErrorLogin(){
            loginView.setEnabled(true);
            loginView.isFocusableInTouchMode();

            passwordView.setEnabled(true);
            passwordView.isFocusableInTouchMode();

            loginView.setCursorVisible(true);
            passwordView.setCursorVisible(true);
}

This should works.

Related