2

I'm building a login style application within android studio. As part of this I have created a rule that if the counter which monitors the number of invalid login attempts, gets to 0. I've currently got it that when the counter gets to 0, the login button is disabled. I was wondering if there is any way of disabling this button for a set period of time, rather than for infinity?

switch (counter) {
    case 0:
        b1.setEnabled(false);
    break;

    case 1:
         tx1.setBackgroundColor(Color.RED);
    break;

    case 2:
        tx1.setBackgroundColor(Color.YELLOW);
    break;
}
JGallardo
  • 11,074
  • 10
  • 82
  • 96
Scarlett
  • 131
  • 1
  • 2
  • 8
  • You would also have to consider what happens when the orientation changes and when the user closes the app and restarts it. Does the counter reset, does the timer end early etc? – stefana Feb 26 '16 at 10:11
  • When the user restarts the app the counter is reset to 3. I don't mind that everything resets when the user refreshes the application, as long as while it is still open the button being disabled for a period of time works. – Scarlett Feb 26 '16 at 10:16
  • Anyone seeing this question also see https://stackoverflow.com/questions/4384890/how-to-disable-an-android-button?rq=1 – JGallardo Jul 16 '18 at 21:51

1 Answers1

3

Use following code. it will disabled button after 5 seconds when activity or fragment start when you put in OnCreate() method.

new Handler().postDelayed(new Runnable() 
    {
        public void run() 
        {
            b1.setEnabled(false);
        }
    }, 5000    //Specific time in milliseconds
);
Strider
  • 4,452
  • 3
  • 24
  • 35
sasikumar
  • 12,540
  • 3
  • 28
  • 48