1

I have the following code, It is for a login application which redirects to a website. I cannot find specific help online to disable the login_btn for a certain time period and then enable again with the attempts_remaining back to the initial 3. Any help much appreciated

public class MainActivity extends AppCompatActivity {

EditText username;
EditText password;
TextView attempt_count;
Button login_btn;
int attempts_remaining = 3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    username = (EditText) findViewById(R.id.editText_user);
    password = (EditText) findViewById(R.id.editText_password);
    attempt_count = (TextView) findViewById(R.id.textView_attempts_count);
    login_btn = (Button) findViewById(R.id.button);



    login_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if (username.getText().toString().equals("admin") && password.getText().toString().equals("password")) {
                Toast.makeText(getApplicationContext(), "Redirecting ...", Toast.LENGTH_SHORT).show();
                Intent website = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.uk"));
               startActivity(website);


            } else {
                Toast.makeText(getApplicationContext(), "Wrong Username or Password", Toast.LENGTH_SHORT).show();


                attempt_count.setVisibility(View.VISIBLE);
                attempt_count.setTextColor(Color.RED);
                attempts_remaining--;
                attempt_count.setText(Integer.toString(attempts_remaining));


                if (attempts_remaining == 0) {

                    Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "Please Wait", Toast.LENGTH_SHORT).show();
                    login_btn.setEnabled(false);


                }
            }

        }
    });

}

}

FlyingPumba
  • 1,015
  • 2
  • 15
  • 37

3 Answers3

0

You can add a simple piece of code below your disable code

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            login_btn.setEnabled(true);
            attempts_remaining = 3;
        }
    },10000);
Stefan
  • 2,098
  • 2
  • 18
  • 29
0

based on this SO question, you could try the following:

            if (attempts_remaining == 0) {
                Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "Please Wait", Toast.LENGTH_SHORT).show();
                login_btn.setEnabled(false);
                login_btn.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                      login_btn.setEnabled(true);
                      attempts_remaining = 3;
                   }
                }, 10 * 1000); // 10 seconds
            }
Community
  • 1
  • 1
FlyingPumba
  • 1,015
  • 2
  • 15
  • 37
0

You could try this,

what you need is a handler, From Android's developers page https://developer.android.com/reference/android/os/Handler.html

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own

change your onClick method to this

@Override
    public void onClick(View v) {


        if (username.getText().toString().equals("admin") && password.getText().toString().equals("password")) {
            Toast.makeText(getApplicationContext(), "Redirecting ...", Toast.LENGTH_SHORT).show();
            Intent website = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.co.uk"));
           startActivity(website);


        } else {
            Toast.makeText(getApplicationContext(), "Wrong Username or Password", Toast.LENGTH_SHORT).show();


            attempt_count.setVisibility(View.VISIBLE);
            attempt_count.setTextColor(Color.RED);
            attempts_remaining--;
            attempt_count.setText(Integer.toString(attempts_remaining));


            if (attempts_remaining == 0) {

                Toast.makeText(getApplicationContext(), "Login Failed", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "Please Wait", Toast.LENGTH_SHORT).show();
                login_btn.setEnabled(false);

new Handler().postDelayed(new Runnable() 
{
    @Override //depending on if you are inheriting from a super class
    public void run() 
    {
        login_btn.setEnabled(true);
    }
}, 10000    // time in milliseconds, 10000ms = 10s
);
            }
        }

    }
});

 }
}

You might have to make some adjustments to fit you code, let me know how that went.

Kennedy
  • 547
  • 4
  • 23