Search code examples
androidandroid-activityjava.util.concurrent

Refresh TextView in Android


I try to refresh TextView in Android. I am using ExecutorService in my code. When I pushed the "Start refresh" button, after several seconds my app has been stopped by Android system. I have read several topics in stackoverflow.com, but I still cannot understand what I am doing wrong. Here is my code:

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class MainActivity extends Activity {
    private static ExecutorService exec;
    private static TextView textView;
    private static Calendar cal;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.text_field);
        cal = new GregorianCalendar();
        exec = Executors.newSingleThreadExecutor();
    }

    private static class Task implements Runnable {
        @Override
        public void run() {
            try {
                while(!Thread.currentThread().isInterrupted()) {
                    textView.setText(cal.getTime().toString());
                    TimeUnit.SECONDS.sleep(1);
                }
            }
            catch (InterruptedException e) {
                return;
            }
        }
    }

    public void finish_refresh(View view) {
        exec.shutdownNow();
    }

    public void start_refresh(View view) {
        exec.submit(new Task());

    }
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <Button android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="@string/start_refresh"
        android:onClick="start_refresh"/>

    <Button android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="@string/finish_refresh"
        android:onClick="finish_refresh"/>

    <TextView android:layout_height="200dp"
        android:layout_width="200dp"
        android:layout_marginTop="10dp"
        android:id="@+id/text_field" />

</LinearLayout>

Solution

  • You need to use runOnUiThread() Add following code in your onCreate() method:

    Thread t = new Thread() {
        @Override
        public void run() {
            try {
                 while (!isInterrupted()) {
                        Thread.sleep(1000);                  
                        runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                              // update TextView here!
                              textView.setText(cal.getTime().toString());
                          }
                     });
                 }
              } catch (InterruptedException e) { } 
         }
    };
    
    t.start();