12

How to dynamically change the content of a TextView from another part of the screen?

I have a TabActivity class that draws a RelativeLayout that contains a TextView followed by a with several tabs. Within each tab is a separate Intent. From one of the tab intents, I would like to change the text (via .setText) of the TextView from the parent TabActvity.

Is this possible?

ehymel
  • 1,360
  • 2
  • 15
  • 24

4 Answers4

27

You should use Android Architecture Components :

You can create a ViewModel containing LiveData of your data object (LiveData<String> in case you want to change only text).

When you will change your live data object from one Activity or Fragment all other Activities and Fragments observing for this live data object will get notified.

Official API doc has complete example with description.

Vaibhav Jani
  • 12,428
  • 10
  • 61
  • 73
  • 4
    Be wary that this will change that `TextView` for every instance of that class on the stack. – Jason Robinson Jul 07 '11 at 17:45
  • 5
    "Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)" - Not a better solution anymore. You will get this warning in Android Studio while making it static. Any better solution? – Anees U Jun 18 '16 at 07:50
  • Gives me a NullPointerException. `Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference` – Shailesh Oct 21 '16 at 09:47
  • @AneesU Agree, updated outdated and incorrect answer :) – Vaibhav Jani Jul 06 '17 at 02:01
6

Make a public method in your TabActivity that sets the TextView's text, then call getParent() from the child activity, cast it to your TabActivity class, then call that public method.

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • Wow, this is a great idea, keeping everything in the TabActivity class. Will definitely try this as well. – ehymel Jul 07 '11 at 15:38
0

In a case of changing text from asynctask file, you need to implement an interface with a listener. Example:

AsynctaskFile:

OnReadyListener onReadyListener;

public class ABCAsynctaskFile{

   ...

   onReadyListener.onReady();

}

public interface OnReadyListener{

void onReady();

}


public void setOnReadyListener(OnReadyListener onReadyListener){

this.onReadyListener = onReadyListener;

}

ActivityFile:

public class ABC extends AppCompactActivity implements ABCAsynctaskFile.OnReadyListener{
   ..

   ABCAsynctaskFile aBCAsynctaskFileObj = new ABCAsynctaskFile(context);

   aBCAsynctaskFile.setOnReadyListener(ABC.this)

}

@Override

public void onReady(){

   // Your wished changed in edit text.

}

This structure will help you to prevent null pointer exception.

Hristo Eftimov
  • 13,845
  • 13
  • 50
  • 77
0

You could try implementing a handler for the parent Tab which does the job. Pass the text in a message object from each of your respective tabs. To be safe, make changes within the handler inside a runOnUI block

Ujwal Parker
  • 682
  • 7
  • 11
  • 1
    Thanks... I actually thought of that and tried it but probably implemented it incorrectly. Good suggestion. – ehymel Jul 07 '11 at 15:36