1

I want to change TextView's string at 5 second intervals. So I wrote:

    Intent intent = getIntent();
    int kutisu = Integer.parseInt(intent.getStringExtra("kutisu"));// the number of times
    int max = Integer.parseInt(intent.getStringExtra("max"));
    int speed = 5000;// 5 seconds

    int[] sum=new int[kutisu]; // After finished count, I want to ask for a total  
    int answer = 0;
    for(int i=0;i<=kutisu;i++){
        sum[i]=new java.util.Random().nextInt(max);
        tv.setText(sum[i]); //tv is a TextView
        try {
            Thread.sleep(speed); // stop
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

But the string doesn't changed when running it. How do I change TextView's string every 5 seconds?

ra1ned
  • 337
  • 5
  • 19

5 Answers5

2

use a timerTask

public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job --> change yourView text data
initializeTimerTask();
//schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
timer.schedule(timerTask, 5000, 10000); //
}

you can use postDelayed too ,to keep it running continuously at an interval of 5 second, you need to call postDelayed as nested in your Run method again

handler.postDelayed(new Runnable() {

  public void run() {
    Log.d("MyActivity", "Ding Ding");
     // --> change yourView text data
    //calling postdelayed again
    handler.postDelayed(this, 5000); //added this line
  }
}, 5000);
Charuක
  • 12,953
  • 5
  • 50
  • 88
0
   Handler handler = new Handler();
   private Runnable myRunnable=new Runnable(){
    public void run(){
        //Do what you want to your textView here
        handler.postDelayed(this, 5000);      
    }
};
T.S
  • 911
  • 8
  • 24
0

You can use 2 TextView on the same place

private void startFirstFadeOut() {
        Animation animation = new AlphaAnimation(1f, 0f);
        animation.setDuration(ANIMATION_DURATION);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                firstTextView.setVisibility(INVISIBLE);
                startSecondFadeIn();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        firstTextView.setAnimation(animation);
        firstTextView.animate();
    }

    private void startSecondFadeIn() {
        Animation animation = new AlphaAnimation(0f, 1f);
        animation.setDuration(ANIMATION_DURATION);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                secondTextView.setVisibility(VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startSecondFadingOut();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        secondTextView.setAnimation(animation);
        secondTextView.animate();
    }

    private void startSecondFadingOut() {
        Animation animation = new AlphaAnimation(1f, 0f);
        animation.setDuration(ANIMATION_DURATION);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                secondTextView.setVisibility(INVISIBLE);
                startFirstFadingIn();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        secondTextView.setAnimation(animation);
        secondTextView.animate();
    }

    private void startFirstFadingIn() {
        Animation animation = new AlphaAnimation(0f, 1f);
        animation.setDuration(ANIMATION_DURATION);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                firstTextView.setVisibility(VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startFirstFadeOut();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        firstTextView.setAnimation(animation);
        firstTextView.animate();
    }

Just call startFirstFadeOut and everything gonna be automatically working

Omar HossamEldin
  • 3,033
  • 1
  • 25
  • 51
0

I use this way:

`String[] array={
             "man","for","think", 
    };`

TextView t = findViewById(R.id.textView);

         new CountDownTimer(5000,1000) {

             @Override
             public void onTick(long millisUntilFinished) {}

             @Override
             public void onFinish() {
                 t.setText("Your Text "+array[i] +" Your text");
                 i++;
                 if(i== array.length-1) i=0;
                 start();
             }
         }.start();

enter image description here

enter image description here

imxitiz
  • 3,920
  • 3
  • 9
  • 33
Mori
  • 2,653
  • 18
  • 24
0

I prefer this way...

Handler text_changer = new Handler();
        text_changer.postDelayed(new Runnable() {

            public void run() {
                TextView txt = findViewById(R.id.txt);
                if (txt.getText().equals("First_Text")) {
                    txt.setText("Second_Text");
                    text_changer.postDelayed(this, 5000);
                } else if (txt.getText().equals("Second_Text")) {
                    txt_footer.setText("First_Text");
                    text_changer.postDelayed(this, 5000);
                }
            }
        }, 5000); 
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 13 '22 at 08:43