2

Attached to my EditText is a new TextView.OnEditorActionListener()

public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {

 if(i == EditorInfo.IME_ACTION_DONE){

  dataSnapshot.child("add").child(child.getKey()).child("comment").getRef().setValue(String.valueOf(serviceComment.getText()));
  return true;

 }else{

  return false;

 }

}

After execution, the text inside my EditText is cleared when I want it to persist.

Additionally, after it is cleared, and I type in the same exact thing, the code doesn't execute and the keyboard doesn't disappear until I type in something different from what I previously typed.

My ultimate goal is to have the text within my EditText to persist after clicking done and hiding the keyboard.

Here is the XML for my EditText

<EditText
        android:id="@+id/txtComment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorSecondary"
        android:hint="Enter a comment"
        android:imeOptions="actionDone"
        android:maxLength="200"
        android:maxLines="1"
        android:singleLine="true"
        android:textColor="@color/colorBlack"
        android:textSize="14sp" />
Garren Fitzenreiter
  • 781
  • 1
  • 5
  • 22

1 Answers1

0

To hide the keyboard you have to perform this inside the listener callback:

InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

Please try it.

Also, the Edit Text clear issue might be due to the line: dataSnapshot.child("add").child(child.getKey()).child("comment").getRef().setValue(String.valueOf(serviceComment.getText()));

This line might be causing UI Hang and can be the reason.

Try calling that operation in another thread.

Harish Jose
  • 326
  • 3
  • 9
  • Thanks for the reply, but my problems is that my EditText is being cleared after hitting the "done" button on the keyboard – Garren Fitzenreiter Sep 13 '18 at 05:07
  • I have a doubt: The EditText code you've shared is having width 0dp but there is no weight mentioned. How it could be? just curious – Harish Jose Sep 13 '18 at 05:09
  • 1
    0dp when attached to a constraint layout matches constraint – Garren Fitzenreiter Sep 13 '18 at 05:12
  • Could you try once by commenting the dataSnapshot.child("add").child(c....... line? – Harish Jose Sep 13 '18 at 05:17
  • 1
    After commenting that line out, the text stays, but my code wont get executed. Hmm. why would it clear the text after executing that code? – Garren Fitzenreiter Sep 13 '18 at 05:24
  • Ah Yes! I think that's a db operation and since we don't know whats the logic you are doing, I can't say a solution. Could you check the logcat, may be its doing too much work and UI is hanged. – Harish Jose Sep 13 '18 at 05:31
  • I believe you're right. I don't see why it would clear my text or appear to clear it. Perhaps it just appears to clear it. I'll try writing a function for the database and call it from the onEditorAction and see if that works. – Garren Fitzenreiter Sep 13 '18 at 05:39
  • Try calling that operation in separate thread using a Handler or something, which will definitely solve the issue. – Harish Jose Sep 13 '18 at 05:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179953/discussion-between-harish-jose-and-garren-fitzenreiter). – Harish Jose Sep 13 '18 at 06:00
  • Still not a solution. Anytime the db code is called, it clears out the edittext – Garren Fitzenreiter Sep 13 '18 at 06:01