0

I am trying to change a TextView from another thread in another class, but I am not sure how to best solve this.

I am making an instant messaging application and when receiving a message through an input stream I want to change the view which is set in another class.

Should a listener be implemented to eg handle it like this?

  1. Client 1 reads a message through message = inputstream.readObject() in class A

  2. An Observer/Listener function in class B is called through class A and sets a TextView with the message value.

Thanks!

Ellen W
  • 85
  • 5

1 Answers1

1

The best solution - to use custom Listener interface - How to create our own Listener interface in android?

Dont forget, that ui can be changed only from MAIN thread. If you have access to activity, you can use:

activity.runOnUiThread(() -> {
//change the view
});

If you dont:

new Handler(Looper.getMainLooper()).post(() -> {/*change*/ });
Yegorf
  • 400
  • 2
  • 11