0

I have an EditText in my application. This editText is being updated through code for every 10 seconds using editText.setText method. I want to stop this updation when user opens soft keyboard. When user completes his typing action (Press done) button I want to resume updation from editText.setText. I have tried InputMethodManager isActive(View) to check whether my EditText is currently active while using editText.setText method. This perfectly works when keyboard is displayed. But when user clicks done button and soft-keyboard gets hidden, isActive(View) still gives true and my editText.setText is not being called. Below is my code which updates editText every 10 seconds.

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(!imm.isActive(editText)){
   editText.setText("10.2");
  }

So this imm.isActive(editText) returns true even after user press done button of soft keyboard. Please suggest any way to get this done.

1 Answers1

0

Solution 1

Create a boolean control variable isKeyboardVisible and set it to true when you show the keyboard, false when you hide it. Check its value before calling setText().

Solution 2

An alternate, somewhat hacky solution would be to enclose both of the portions that change the EditText value within a synchronized block. This way each portion would wait for the other to complete before executing.

Solution 3

You can listen to the EditText's focus change events and react to them. They tend to coincide with the soft keyboard showing/hiding itself.

Community
  • 1
  • 1
Piovezan
  • 3,215
  • 1
  • 28
  • 45
  • No problem. If you are satisfied with the answer, accept it by clicking on the checkmark at the left of it. – Piovezan Oct 22 '13 at 16:08
  • I was trying solution 1, however there are tricky ways to find out visibility of keyboard. In many threads they have suggested to measure size of screen before and after visibility of keyboard. I can't use this approach because I am running this app in full screen mode. Solution 2 could be useful, but when user type into editText from keyboard, I don't have control over this code and I can't put this code in synchronized block. – Abhijeet Duraphe Oct 22 '13 at 16:14
  • @user2907714 I have added a third solution. Not sure if it will work. Hope it helps. – Piovezan Oct 22 '13 at 16:57
  • When I am pressing done button,editText doesn't loose focus. I am trying to set focus on another button component but editText still have focus. – Abhijeet Duraphe Oct 22 '13 at 20:16
  • I see. I have no other ideas. Maybe if I delete my answer entirely the question can be made available for other members to answer? – Piovezan Oct 23 '13 at 10:54
  • One last idea: Does listening to editText's text changes correspond to pressing Done in your app? – Piovezan Oct 23 '13 at 10:57