1

So in my app I want to have some click on a text view, the text change, have a 3 second delay then have the text change back to the original. Here is what i have so far.

        final TextView fact1 = (TextView) findViewById(R.id.fact1);
        fact1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fact1.setText("Text");
            //Delay
            fact1.setText(Original text here, its stored in a text view with the id fact1);

        }
    });

How would i go about having a delay inbetween the text view switching?

  • SImple Thread.sleep. Use animaton, use timer or check `android:hint` in layout xml – Rafal May 22 '16 at 20:40
  • Any documentation to help? –  May 22 '16 at 20:41
  • https://developer.android.com/training/animation/index.html or simpler new Handler(Looper.getMainLooper).postDelayeD(new Runnable ( //change you text here), YOUR_DELAY)); http://stackoverflow.com/questions/5623578/android-delay-using-handler – Rafal May 22 '16 at 20:42
  • If you just search for a minute found several method... – Saeed Darvish May 22 '16 at 20:53

3 Answers3

6

No reason to create a new Thread for this. Just use postDelayed on the TextView.

fact1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String originalText = fact1.getText().toString();
        fact1.setText("New Text");
        fact1.postDelayed(new Runnable() {
            @Override
            public void run() {
                fact1.setText(originalText);
            }
        }, 3000);
    }
});
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
  • That is the "correct" answer. Do yourself a favour and do not do any of those non-sense Thread or CountDownTimer. For your simple use case it certainly does not apply. Gerge, I put correct in quotation mark because I wouldn't code it like this as it can potentially create a memory leak. – Budius May 22 '16 at 21:14
  • 1
    @Budius Just to be sure are you referring to the leak of `fact1` and its `Activity`, etc by the `Handler` used internally by `postDelayed`? Typically I'd use my own handler and clear it when I know the `Activity` is moving to background or no longer used but in this case it should only be leaked for roughly the 3 seconds at most right? – George Mulligan May 22 '16 at 21:19
  • Yes. That's exactly what I'm referring to. Working in an app with 700K MAU makes me code in an extremely preventive way. There's some really weird stuff that happens out there in the wild. So as general rule I don't do anonymous inner classes. Even 3 seconds leak count. – Budius May 22 '16 at 21:24
0

You can use CountDownTimer like this :

String firstText=mTextField.getText().toString();
  new CountDownTimer(30000, 1000) {       
    public void onTick(long millisUntilFinished) { 

           mTextField.setText("seconds remaining: " +               millisUntilFinished / 1000); //here you can have your logic to set text to edittext

   } 

public void onFinish() { 
mTextField.setText(firstText); }

 }.start();
Saeed Darvish
  • 621
  • 6
  • 29
  • How would i get the text back to the original text when i am done? I want it to change for 5 second then go back to what it was. Here is what i have. –  May 22 '16 at 20:57
  • public void onClick(View v) { Handler handlerTimer = new Handler(); new CountDownTimer(30000, 1000) { public void onTick(long millisUntilFinished) { fact1.setText("Memes"); } public void onFinish() { fact1.setText(); } }.start(); } –  May 22 '16 at 20:58
  • Before you start timer change Text and in onFinish method retrive last text.. you can save first text in string variable. – Saeed Darvish May 22 '16 at 21:01
  • How would i do that? –  May 22 '16 at 21:07
0

you only need to run the code in a thread and call Thread.sleap

    Button button = (Button) findViewById(R.id.button);
    final TextView textView = (TextView) findViewById(R.id.textView2);
    final String[] originalText = {""};
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Thread() {
                @Override
                public void run() {
                    originalText[0] = textView.getText().toString();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText("what ever!!");
                        }
                    });
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText(originalText[0]);
                        }
                    });
                }
            }.start();
        }
    });

by using lambda the code will be much more concise

    Button button = (Button) findViewById(R.id.button);
    final TextView textView = (TextView) findViewById(R.id.textView2);
    final String[] originalText = {""};
        button.setOnClickListener(view -> {
        new Thread() {
            @Override
            public void run() {
                originalText[0] = textView.getText().toString();
                runOnUiThread(() -> textView.setText("what ever!!"));

                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                runOnUiThread(() -> textView.setText(originalText[0]));
            }
        }.start();
    });
humazed
  • 74,687
  • 32
  • 99
  • 138