This is because the UI thread runs separately to your Runnable
thread. Separate threads like this cannot interact. You will need to use an AsyncTask
thread which will enable you to periodically access the UI thread.
For example:
private class ExampleThread extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {}
@Override
protected void doInBackground(Void... params) {
while(!isCancelled()) { // Keep going until cancelled
try {
Thread.sleep(100); // Delay 100 milliseconds
} catch (InterruptedException e) {
Thread.interrupted();
}
publishProgress(); // Run onProgressUpdate() method
if(isCancelled()) break; // Escape early if cancel() is called
}
}
@Override
protected void onPostExecute(Void... params) {}
@Override
protected void onProgressUpdate(Void... params) {
// Here you can access the UI thread
Draw();
}
}
To start the thread:
ExampleThread thread = new ExampleThread();
thread.execute();
To stop the thread:
thread.cancel();
More information and examples with AsyncTask can be found on this question.