0

I want to empty an EditText based on a timer, according to the following behaviour:

At time t1, the user starts typing. The EditText should be cleared at time t1 + 10 seconds.

Once the EditText has been cleared, the timer should stop.

When the user types in it again, at time t2, the field should be cleared at t2 + 10 seconds, and so forth.

I tried using a Handler in the afterTextChanged method in the TextChangedListener.

However, this is not giving the desired behaviour: the Handler is started when the user starts to type but from then on, it keeps clearing the text every 10 seconds, even when the user hasn't typed anything (which seems obvious from the way I have coded).

But I do not know how I can change it so that the text gets clears after 10 seconds from when the user starts typing something, rather than every 10 seconds non-stop.

Isn't there a way to use a Timer, that's better than using Handlers? Handlers seem like a thread running in parallel once triggered.

ataulm
  • 15,195
  • 7
  • 50
  • 92
user900785
  • 423
  • 3
  • 14
  • 32

2 Answers2

0

Using a Timer...

How to set a timer in android and
http://writecodeeasy.blogspot.com/2012/08/androidtutorial-timer-p1.html

you can setup the timer to run only once after specific seconds too.! Try it Hope it helps!

Community
  • 1
  • 1
Araib karim
  • 421
  • 4
  • 16
  • I think my problem is, even if I create a timer, I need to cancel it and restart it. I need to clear text after 10 seconds from when the user starts typing and somehow when I called the timer withing the onTextChanged, and cancel it in afterTextChanged, nothing happenS! – user900785 Jul 03 '13 at 10:08
  • Timer is the best thing that can achieve your requirement.. make sure ur putting the right values for the date. u can set up Toast or Log methods to check if the time really started and executed the method you want. – Araib karim Jul 04 '13 at 06:36
0

Try this,

EditText text = (EditText) findViewById(R.id.YOUR_ID);
text.addTextChangedListener(textWatcher);
Handler myHandler = new Handler();
Runnable myRun = new Runnable(){
 public void run() 
        {
            text.setText("");
        }   
};
private TextWatcher textWatcher = new TextWatcher() {

  public void afterTextChanged(Editable s) {
         myHandler.postDelayed(myRun,10000);
  }

  public void beforeTextChanged(CharSequence s, int start, intcount, int after) {
  }

  public void onTextChanged(CharSequence s, int start, int before,
          int count) {
   myHandler.removeCallbacks(myRun);

  }
}
No_Rulz
  • 2,679
  • 1
  • 20
  • 33
  • This is exactly what I tried after Raghunandan's post. However Nothing happens. As in the text field is not cleared – user900785 Jul 03 '13 at 10:00
  • myHandler.removeCallbacks(myRun); is in onTextChanged – No_Rulz Jul 03 '13 at 10:20
  • If the handler callback is called during onTextChanged, how will the handler be even allowed to run during afterTextChanged?? – user900785 Jul 03 '13 at 10:32
  • While enter a text in edit text the onText will called so I removed the handler, after calling the onText it must call afterText there I will start the handler from this it will start the timer as 10s – No_Rulz Jul 03 '13 at 10:58
  • Thanks No_Rulz. I got the idea. However, the time just elapses after the text appears. Infact the EditText box that I have used is simply for "displaying". It is not of input type. So I dont know how relevant it is. What about TextView. can I do something like this? – user900785 Jul 03 '13 at 11:57
  • Then how did you put the text in Edit – No_Rulz Jul 03 '13 at 12:06
  • I mean I used the text as EditText and set it with text.setText(".."). However the point is this text is visbile when the user clicks a button. The only thing I want to take care is that it goes away in 10 seconds from the time it is shown. – user900785 Jul 03 '13 at 12:34
  • Ohh after clicking the button you want to start a timer. not at editing your edit text am i correct? If it correct means in btnClickListener you will call your handler – No_Rulz Jul 03 '13 at 12:43