Disable edit text field in android for 30 seconds - android

I am making a login screen for an android app. If a user enter wrong password credential 3 times, then the edit text field will be disable for 30 second. After 30 seconds, user can input their password again. How can i achieve it? Thank you

you can try
//Disable your EditText
Handler handlerTimer = new Handler();
handlerTimer.postDelayed(new Runnable(){
public void run() {
// Enable it
}}, 30000);
But I think it's not a good solution to block an edit text 30 seconds, why would you do that?
I hope that help you.

int start1=18000;
final Timer timer = new Timer();
new Thread(new Runnable() {
#Override
public void run() {
timer.schedule(new TimerTask() {
#Override
public void run() {
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
if (start1 <= 0) {
timer.cancel();
timer.purge();
editTextActivation.setEnabled(true);
}
start1 = start1 - 1000;
}
});
}
}, 0, 1000);
}
}).start();

Well first you'll have to have a counter that increments every time a wrong password is submitted. A simple int field would do the trick:
int passwordAttempt = 0;
Next, you'll have to increment this "counter" when a user has entered a wrong password. If the counter reaches 3, disable your EditText (and re-enable it after 30 seconds):
if(passwordAttempt == 3) {
// User has entered wrong password 3 times.
// Disable your EditText.
yourEditText.setEnabled(false);
// Re-enable your EditText after 30000 milliseconds (30 seconds).
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yourEditText.setEnabled(true);
}
}, 30 * 1000);
// Reset the counter.
passwordAttempt = 0;
}

Try this out...
Thanks to #ridsatrio, I have UPDATED the code.
editText.setEnabled(false);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setVisibility(View.GONE);
editText.setEnabled(true);
}
}.start();

Related

Why isn't my text string updating?

So I am trying to display the time passed since pressing a button on my app.
My code is:
/*This will initiate the timer*/
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
start=System.currentTimeMillis();
time=System.currentTimeMillis()-start;
currenttimedisplay.setText(Long.toString(time));
}
});
}
}, 0, 1000);
The app runs but when I press the button it just shows "0.0".
The app doesn't close out. Any help is appreciated!
Please try this:
/*This will initiate the timer*/
timer = new Timer();
start=System.currentTimeMillis();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
time=System.currentTimeMillis()-start;
currenttimedisplay.setText(Long.toString(time));
}
});
}
}, 0, 1000);
In your code, the value of start is changed everytime the timer elapsed (start = System.currentTimeMillis()), so the value of time is always 0 (System.currentTimeMillis() - System.currentTimeMillis() should be 0 if it is called with no, or very small time difference...). So you should set the value of start on button press, then calculate the difference, and update text view in your timer task.

Timer switch android TimerTask

b= new TimerTask()
{
#Override
public void run()
{
out_string = hi.getWebPage("http://daniandroid.honor.es/getAllCustomers.php");
frum_timer = Integer.parseInt(out_string.replaceAll("[\\D]",""));
};
};
t.scheduleAtFixedRate(b, 500, 1000);
Every 1000 ms, run() will store the value to int frum_timer
I'm using this for an flashlight application.
When I open the app, I want my app to switch on and off flashlights using frum_timer for interval time.
So if
frum_timer = 1000;
my flashlight will turn on and off every 1 sec.
and if I edit to
frum_timer = 300;
it will turn on and off every 0.3 sec.
I tried instantiate a new TimerTask()
c=new TimerTask()
{
#Override
public void run()
{
timetask = new TimerTask()
{
boolean checking = false;
#Override
public void run()
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
if(checking==true)
{
//camera turning on
checking=false;
}else{
//camera turninf off
checking=true;
}
}
});
}
};
}
};
//some code
t.scheduleAtFixedRate(c, 50, 700);
but it's quite hard, because when I change the value, a new timer will start and the old one will remain active.
BTW: I know that the code for turning on/off will not change at the same interval as I want because of my amateur programmer skills (if checking...etc)
I have found a way.
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Android - TextView with dynamic text

For a simple Timer App I try to add a TextView to my app that says something like "15 seconds remaining", obviously this changes every seccond until it finally changes to "time up!". My idea was to use a loop like this:
while(now<endTime):
remaining=endTime-now;
text.setText(remaining);
While this could work, I am unsure if this is the right approach or if theres a better option.
You can use a handler to do it as below:
Handler myHandler=new Handler();
myHandler.postDelayed(timer, 1000);
private Runnable timer= new Runnable() {
public void run() {
if(counter==15){
textview.setText("Time Up");
counter=0;
}
else{
counter++;
textview.setText(counter+ " remaining");}
}
};
Use a CountDownTimer, instead.
I use this code snippet to achive a similar result:
final int secs = 5;
new CountDownTimer((secs +1) * 1000, 1000) // Wait 5 secs, tick every 1 sec
{
#Override
public final void onTick(final long millisUntilFinished)
{
txtCount.setText("" + (int) (millisUntilFinished * .001f));
}
#Override
public final void onFinish()
{
txtCount.setText("GO!");
}
}.start();
In the onFinish() handler you can do the "elapsed time" stuff

Android countdown timer wont accept variable

i just started to learn creating android apps. I wanted to create a simple count down timer that takes a value from a edittext but countdown timer does not seem to run.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countDownTxt = (TextView) findViewById(R.id.countDownView);
intervalTxt = (TextView) findViewById(R.id.intervalText);
findViewById(R.id.startBN).setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
int testInt = 30;
//countDownTxt.setText(intervalTxt.getText());
int interval = Integer.parseInt(intervalTxt.getText().toString());
Log.d("buttonpressed", "interval for countdown is " + interval);
cdt = new CountDownTimer(Integer.parseInt(intervalTxt.getText().toString()), 1000) {
public void onTick(long millisUntilFinished) {
Log.d("counttimer1", "haha1");
countDownTxt.setText(""+ millisUntilFinished / 1000);
}
public void onFinish() {
cancel();
}
}.start();
}
}
);
}
In particular, this program works only if i enter a numerical value such as 30000 in the 1st parameter of the CountDownTimer "cdt = new CountDownTimer(testInt, 1000)"
Can someone enlighten me please? Thank you!
"Doesn't work" how? You should post the error message you're getting or other symptoms of "doesn't work."
What's probably happening is CountDownTimer accepts only long values as the first parameter of its constructor. Not int values.
Change int testInt = 30 to long testLong = 10000.0f and see what happens.
The first parameter means milliseconds, by the way, so "30" isn't really going to get you much in the first place.
onTick() method is called in a separate Thread. But you don't have right to use setText() method outside the GUI Thread.
You must use a Handler object or Activity.postOnUiThread() method to execute something in the GUI Thread :
cdt = new CountDownTimer(Integer.parseInt(intervalTxt.getText().toString()), 1000) {
public void onTick(long millisUntilFinished) {
Log.d("counttimer1", "haha1");
runOnUiThread(new Runnable() {
#Override
public void run() {
countDownTxt.setText("" + millisUntilFinished / 1000);
}
});
countDownTxt.setText(""+ millisUntilFinished / 1000);
}
public void onFinish() {
cancel();
}
}.start();
For more informations, read http://developer.android.com/guide/components/processes-and-threads.html#Threads

implement timer in my app

i am developing a game.i want to display score on button click.but it should be displayed only for few seconds.i want to implement timer in my app.but i dnt knw how to implement that.i searched in the google.but results were confusing me...given below is my code snippet.plz anybody help me...
OnClickListener clickball=new OnClickListener() {
#Override
public void onClick(View v) {
score=scorenumber.nextInt(9);
id=v.getId();
Log.v("", "u clicked me");
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
}
else if(id==R.id.ball3)
{
ball3.setText(Integer.toString(score));
}
else if(id==R.id.ball5)
{
ball5.setText(Integer.toString(score));
}
}
}
Something like
//Show score here
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
//hide score here
}
}, 2000);
will hide your score after two seconds.
No need to go so deep in your case. Every view has postDelayed() method, that will run a custom code in UI thread after a set amount of time (in milliseconds). For example:
ball5.postDelayed(new Runnable(){
#Override
public void run() {
ball5.setText("");
}
}, 3000);
will clear "ball5" text after 3 seconds has passed
Another example
...
if(id==R.id.ball2)
{
ball2.setText(Integer.toString(score));
ball2.postDelayed(new Runnable(){
#Override
public void run() {
ball2.setText("");
}
}, 3000);
}
...
you can use CountDownTimer(); refer the documentation ,
Example :
new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
//hide your score here after 5 secondes (5000/1000)
}
}.start();
I think CountDownTimer is that what you need:http://developer.android.com/reference/android/os/CountDownTimer.html . It's quite simle for implementation
new CountdownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();

Categories

Resources