1

I am currently learning how to develop applications for Android mobile devices.

I wrote a test application to display numbers 0-9 on the device screen. I created a simple function to delay the number change.

However, upon running the application, only the final number is displayed. There is also a delay before this final number shows. I'm assuming that the length of the pause is my defined delay multiplied by the number of digits to be shown.

How do I create an app that changes the numbers with a delay?

public class AndroidProjectActivity extends Activity {
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Main();
    }

void Delay(int Seconds){
    long Time = 0;
    Time = System.currentTimeMillis();
    while(System.currentTimeMillis() < Time+(Seconds*1000));
}

void Main() {
    String ConvertedInt;
    TextView tv = new TextView(this);
    setContentView(tv);

    for(int NewInt = 0; NewInt!= 9; NewInt++){
        ConvertedInt = Character.toString((char)(NewInt+48));
        tv.setText(ConvertedInt);
        Delay(5);
    }
}
Mikael Ohlson
  • 3,074
  • 1
  • 22
  • 28
Schmoopsiepoo
  • 51
  • 1
  • 5
  • Are you emulating this or running on real hardware? – Azulflame Jun 17 '12 at 01:30
  • I'm using the AVD Android emulator. – Schmoopsiepoo Jun 17 '12 at 01:34
  • Try downloading the APK to an actual device – Azulflame Jun 17 '12 at 01:35
  • Please fix the title to get better response. – damned Jun 17 '12 at 03:31
  • do you have a specific reason to want to between outputting values? You could appent them to the TextView instead of simply changing. Then you could see all of your different outputs without the need to add a manual delay. – FoamyGuy Jun 17 '12 at 03:38
  • possible duplicate of [Update UI with Thread sleep](http://stackoverflow.com/questions/10965291/update-ui-with-thread-sleep) – FoamyGuy Jun 17 '12 at 15:01
  • This is a tricky issue that everyone has to deal with. Here is a sample piece of code that demonstrates how to put a time delay inbetween things happening on teh GUI thread. http://stackoverflow.com/questions/10965291/update-ui-with-thread-sleep/10965337#10965337 – DrA Jun 17 '12 at 01:37
  • The link to the tutorial that describes this well is at the link included. But I could include it again here. http://www.vogella.com/articles/AndroidPerformance/article.html#handler – DrA Jun 17 '12 at 13:33
  • The code at the link solves exactly the problem described except that instead so showing numbers with a timed delay inbetween, it makes a button visible / not visible with a timed delay inbetween. Perhaps what you're saying is that I should be declaring this a duplicate question? – DrA Jun 17 '12 at 13:40

3 Answers3

1

Try creating a thread, which sleeps for certain interval of time, and then increment the value by 1 till 9. And use Handler to update the UI.

You can also use AsyncTask

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

One way of doing this is to create a runnable that updates your view. This will still update on the UI thread, but wait in the background. There might be mistakes in the below code, but it should run with minor tweaks..

Blocking in any of the system calls into your activity is not good, since you're blocking the UI thread. Your app will be force closed, with an Application Not Responding message. Here is another good example.

public class AndroidProjectActivity extends Activity {
    private Handler mHandler;
    private TextView mTextView;
    private Runnable mCountUpdater = new Runnable() {
        private int mCount = 0;
        run() {
           if(mCount > 9)
               return;
           mTextView.setText(String.valueOF(mCount+48));
           mCount++;
           // Reschedule ourselves.
           mHandler.postDelayed(this, 5000);
        }
    }
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        // Cleaner to load a view from a layout..
        TextView tv = new TextView(this);
        setContentView(tv);
        mTextView = tv;
        // Create handler on UI thread.
        mHandler = new Handler();
        mHandler.post(mCountUpdater);
    }
}
Mikael Ohlson
  • 3,074
  • 1
  • 22
  • 28
0

The call to main() i blocking the UI so it can not display nay numbers until the call is finished.

MikeIsrael
  • 2,871
  • 2
  • 22
  • 34