Issue
Currently, I'm developing pattern lock application.In my application when user enter wrong pattern on that time pattern lock activity delay 30 second.And i am adding one textview on pattern lock activity that can display only text like "Please Wait 30 Seconds". So I want to replace that text "Please Wait 30 Seconds" in to the decrement counter.
Example: 30-29-28-27-----0 (automatically decrement counter value)
Here this my Textview xml:
<TextView
android:id="@+id/txtMessage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Please Wait 30 Seconds"
android:gravity="center"
android:textSize="25dp"
android:textColor="@color/colorAccent"
android:visibility="gone"/>
See android:text="Please Wait 30 Seconds"
where I want to set decrement counter
This my activity code:
private void invalidPattern() {
failedCount ++;
if (failedCount > LIMIT) {
lock9View.setVisibility(View.GONE);
txtMessage.setVisibility(View.VISIBLE);
Animation zoomOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation);
txtMessage.startAnimation(zoomOutAnimation);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//reset the failed count
failedCount = 0;
lock9View.setVisibility(View.VISIBLE);
txtMessage.setVisibility(View.GONE);
//Enable the input interface here
}
}, 30000); // 30Sec delay
}
else
{
Log.i("Wrong....", "aa..else.....invalidPattern()");
}
}
So, if anyone know give idea of that.
Solution
I think the good approach for your requirement is to use CountDownTimer. Below is the code, that demonstrates how you can use
XML layout file:
<TextView
android:id="@+id/txtMessage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Please Wait 30 Seconds"
android:gravity="center"
android:textSize="25dp"
android:textColor="@color/colorAccent"
android:visibility="visible"/>
in JAVA file
private TextView countDownTv;
CountDownTimer mCountDownTimer = new CountDownTimer(30 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//this will be called every second.
countDownTv.setText("Please Wait " + (millisUntilFinished/1000) + " seconds.");
}
@Override
public void onFinish() {
//you are good to go.
//30 seconds passed.
}
};
@Override
public void onCreate() {
super.onCreate();
//......
countDownTv = (TextView) findViewById(R.id.txtMessage);
//To start the timer...
mCountDownTimer.start()
}
Answered By - Multidots Solutions