prasad.gai
prasad.gai

Reputation: 2987

how to print 1 to 10 numbers at text view in android?

i am beginer to android,i would like to print 1 -10 numbers in order like one by one as shown follows

1 2 3 4 5 6 7 8 9 10

i would like to print those numbers like 1 3secs after 2, 3secs after 3,3secs after 4......so on

i have written code as follows to print 1-10 but it displaying only 10 .

@Override
    public void onClick(View v) {
        name=((TextView)findViewById(R.id.textView1));

        for(int i=0;i<11;i++){
        name.setText("hai"+i);
        }

please is there any solutions for that?

Upvotes: 0

Views: 4751

Answers (2)

Heiko Rupp
Heiko Rupp

Reputation: 30994

This does not work, like this, as onClick() is run on the UI thread, blocking it for updates until your onClick() returns. While

You can solve that with an AsyncTask, where the doInBackground() method is doing the loop, sending the number to print via publishProgress() and have onProgressUpdate() update the TextView.

Here is an example of the usage of AsyncTask. Also have a look at the docs which also illustrates progress updates.

Upvotes: 2

mudit
mudit

Reputation: 25534

There is a function is textview called append. You can this function for the purpose:

On how to use this function:

name.append("\n" + i);

For printing them after an interval, you can Handler and runnable combo OR asynctask.

Upvotes: 2

Related Questions