Changing text from another activity
Asked Answered
S

4

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?

Savory answered 7/7, 2011 at 3:48 Comment(0)
A
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.

Awed answered 7/7, 2011 at 4:46 Comment(4)
Be wary that this will change that TextView for every instance of that class on the stack.Haydon
"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?Isaacisaacs
Gives me a NullPointerException. Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object referenceSearby
@AneesU Agree, updated outdated and incorrect answer :)Awed
H
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.

Haydon answered 7/7, 2011 at 3:52 Comment(1)
Wow, this is a great idea, keeping everything in the TabActivity class. Will definitely try this as well.Savory
C
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

Canzona answered 7/7, 2011 at 4:7 Comment(1)
Thanks... I actually thought of that and tried it but probably implemented it incorrectly. Good suggestion.Savory
W
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.

Walkerwalkietalkie answered 7/6, 2017 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.