Search code examples
androidandroid-edittextandroid-alertdialog

Android How to keep text in EditText when taping outside?


When I type in an EditText and the tap in another part of the screen, the text in the EditText disappears (as the keyboard does when it closes/hides).

The EditText is a password input and is currently being displayed in an AlertDialog.Builder:

final AlertDialog.Builder passwordInputDialog =  new AlertDialog.Builder(MainActivity.this, R.style.CustomAlertDialog);
TextInputLayout passwordInputLayout           = new TextInputLayout(MainActivity.this);
passwordInputInflater                         = LayoutInflater.from(MainActivity.this).inflate(R.layout.password_input, null);

passwordInputDialog
    .setCustomTitle(passwordInputTextView)
    .setMessage(message)
    .setView(passwordInputLayout)
    .setPositiveButton("OK", onPositiveAlertDialogClick())
    .setNegativeButton("Cancel", onNegativeAlertDialogClick());

final AlertDialog passwordInputShownDialog = passwordInputDialog.show();
final EditText enteredPassword = passwordInputInflater.findViewById(R.id.etPassword);

The EditText has attached a addTextChangedListener event, which is able to perform well any other operation:

enteredPassword.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }

    @Override
    public void afterTextChanged(Editable enteredText) {
        ...
        enteredPassword.removeTextChangedListener(this);
        enteredPassword.setText(enteredText);
        enteredPassword.setSelection(enteredText.length());
        enteredPassword.addTextChangedListener(this);
    }
});

The thing is if there's something I'm doing wrong, or is the default behavior of an EditText within a Dialog "when leaving the input".

How can I do in order to "preserve" the text that has been entered in the EditText right after taping outside of this?


This is what I get when I try to set the text of the EditText on the afterTextChanged function:

2019-11-24 20:20:46.795 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 135477(4MB) AllocSpace objects, 0(0B) LOS objects, 50% free, 9MB/18MB, paused 79us total 113.470ms
2019-11-24 20:20:54.553 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 136023(4MB) AllocSpace objects, 0(0B) LOS objects, 49% free, 10MB/21MB, paused 89us total 128.803ms
2019-11-24 20:21:03.751 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 137630(4MB) AllocSpace objects, 0(0B) LOS objects, 50% free, 12MB/25MB, paused 108us total 162.638ms
2019-11-24 20:21:13.794 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 137787(4MB) AllocSpace objects, 0(0B) LOS objects, 50% free, 14MB/28MB, paused 110us total 153.423ms
2019-11-24 20:21:24.617 20941-20954/com.example.map2 I/om.example.map: Background concurrent copying GC freed 137118(4MB) AllocSpace objects, 0(0B) LOS objects, 50% free, 16MB/32MB, paused 101us total 181.131ms

It freezes the application and I had to kill it.

I understood and realized that what I need isn't to do that. As I wanna save what the user writes in the SharedPreferences to display them again so the user doesn't have to type the things more than once.


Solution

  • by closing alertDialog, all of the data inside will be lost. you can prevent to close alertDialog by :

    passwordInputDialog.setCancelable(false);
    

    or you can save the last value inside of alertDialog and bring it back by showing again. (not recommended)