xyz question
xyz question

Reputation: 23

Change TextView value every second (Android)

i am creating a sample app in which i want to change a TextVeiw value every second.

TextView value should change continue every second.

to perform this task i tried this below code:

but this code is not changing text values continue, it only change on app start or screen rotate.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

Boolean b = true;

        final TextView tv = (TextView) this.findViewById(R.id.textView1);

        final String[] str = new String[] { "897451", "34232", "33432",
                "46867", "54554", "6756", "56r7", "2345u8", "9654", "987650", };
        Random generator = new Random();
        final int random = generator.nextInt(str.length);

        Thread t = new Thread() {

            @Override
            public void run() {
                try {
                    while (b != false) {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // update TextView here!
                                tv.setText(str[random]);
                            }
                        });
                    }
                } catch (InterruptedException e) {
                }
            }
        };
        t.start();

    }

and i also tried this code: but this code crash application

private Timer timer = new Timer();
    private TimerTask timerTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        final TextView tv = (TextView) this.findViewById(R.id.textView1);

        final String[] str = new String[] { "897451", "34232", "33432",
                "46867", "54554", "6756", "56r7", "2345u8", "9654", "987650", };
        Random generator = new Random();
        final int random = generator.nextInt(str.length);

         timerTask = new TimerTask() {
         @Override
         public void run() {
         // refresh your textview
         tv.setText(str[random]);
         }
         };
         timer.schedule(timerTask, 0, 1000);
}

Upvotes: 1

Views: 1542

Answers (2)

xyz question
xyz question

Reputation: 23

final TextView tv = (TextView) this.findViewById(R.id.textView1);

        final String[] str = new String[] { "897451", "34232", "33432",
                "46867", "54554", "6756", "56r7", "23458", "9654", "987650", };

        // //
         final Handler h = new Handler();
         h.post(new Runnable() {
         @Override
         public void run() {
         Random generator = new Random();
         final int random = generator.nextInt(str.length);
         tv.setText(str[random]);
         h.postDelayed(this, 1000);
         }
         });
        // //

Upvotes: 1

Don Chakkappan
Don Chakkappan

Reputation: 7560

Try the following code snippet

final Handler h = new Handler();
    h.post(new Runnable() {
        @Override
        public void run() {

             long millis =(long)currentTime();


             dateAndTime.setText(getDate(millis, "dd/MM/yyyy hh:mm:ss.SSS"));

             h.postDelayed(this, 1000);
        }
    });

Upvotes: 3

Related Questions