0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
vishal patel
  • 203
  • 1
  • 5
  • 16
  • `android:text="Please Wait" + failedCount +"Seconds"` – Piyush Oct 14 '16 at 10:12
  • It will never work, try this. – D.J Oct 14 '16 at 10:12
  • @Piyush it can display only limit like int LIMIT = 5; when i'm creating wrong pattern 5 time. I want to decreament value automatically when user enter wrong pattern 5 times....See my question for better understanding – vishal patel Oct 14 '16 at 10:24

3 Answers3

2

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()
    }
0

Use CountDownTimer

private class StoryCountDown extends CountDownTimer {

    public StoryCountDown(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

    @Override
    public void onFinish() {
        mTextField.setText("done!");
    }
}

And use this class like below.

StoryCountDown countDownTimer = new StoryCountDown(30000, 1000);
countDownTimer.start();
Akash Patel
  • 2,757
  • 1
  • 22
  • 30
0

In addition to other answers:

Thread t = new Thread() {
        public void run() {             
            for (int i = 30; i > 0; i--) {                   
                try {
                    final int a = i;
                    runOnUiThread(new Runnable() {
                        public void run() {                        
                            txtMessage.setText("Please Wait "+ i +" seconds");                            
                        }                       
                    });
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();
SpiralDev
  • 7,011
  • 5
  • 28
  • 42