Home > OS >  Update textView by another Thread in Android Java
Update textView by another Thread in Android Java

Time:12-20

I'm new to Java Android and I'd like to know how to update text from TextView from another Thread that extends Thread (and not extends Activity) I've searched and people said to use runOnUiThread. But since my class "myThread" extends Thread, I can't use this method (cuz I should extend from Activity, but I HAVE to extend from Thread).

My thread's code:

public class myThread extends Thread{
    @Override
    public void run() {
        // do my stuff...
        //....
        runOnUiThread(new Runnable(){
            //HOW?????????
        });
    }
}

CodePudding user response:

You don't have to extend from Acitivity class. If you have reference of the related activitiy, you can call methods of this activity.

Scenario 1

If you have Activity class YourActivity, instead writing new class, you can create anonymous threads in your methods.

public class YourAcitivity extends Activity 
{
    
    private TextView view;
    private Thread textViewUpdateThread;
    ...
    ...
    ...

    public void someMethod()
    {

        this.textViewUpdateThread = new Thread(() -> {

            this.runOnUiThread(() -> {
                // Do your stuff here.
            });
            
        });
        this.textViewUpdateThread.start();
    }
}

Scenario 2

You can write your own thread class that accepts target acitivity reference as a constructor parameter. By this way, in your thread class you have acccess to target acitivity that you want to operate on. In this scenario , you have to add related getters / setters to your activity class to acccess gui components such as TextView.

public class YourSpecialThread extends Thread
{

    private Activity targetAcitivity;

    public YourSpecialThread(Activity activity)
    {
        this.targetAcitivity = targetAcitivity;
    }

    @Override
    public void run() {

        this.targetAcitivity.runOnUiThread(new Runnable(){
            // Do your stuff here.
            // Do not forget to add getter methods to your activity for accessing the
            // related GUI components such as your text view.
            this.targetAcitivity.getTextView().setText("....");
        });
    }
}

Scenario 3

Last possible scenario is, you can implement your thread class inside the activity class as a inner static class. By this way, you have access the outer class variables (in your case outer class is Acitivity) and call runOnUiThread method of the Acitivity class.

CodePudding user response:

Another way is to use a handler like following.

public class myThread implements Runnable, Handler.Callback
{
    private WeakReference<TextView> textview;
    private Handler uiHandler;

    public myThread(TextView textview)
    {
        this.textview = new WeakReference<TextView>(textview);
        uiHandler = new Handler(Looper.getMainLooper(), this);
        new Thread(this).start();
    }

    Override
    public void run()
    {
        //Do something
        uiHandler.sendEmptyMessage(0); //Set textview's text.
    }

    @Override
    public boolean handleMessage(Message msg)
    {
        final TextView textview = this.textview.get();
        if(textview == null) return false;
        switch(msg.what) {
            case 0: textview.setText("Hello World"); break;
            case 1: textview.setTextColor(Color.BLUE); break;
            default: return false;
        }
        return true;
    }
}
  • Related