Fatih Bulut
Fatih Bulut

Reputation: 21

change textview when change static string variable in android

This is my main class. change textview settext when static text change in another class. Note: this process will be applied for several views

public class MainActivity extends AppCompatActivity {

        public static String text = "123";
        Button btn;
        TextView txt;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
             txt = findViewById(R.id.txt);
             txt.setText(text);

        }
    }

Another class

public class AnotherClass{  
 public void changeTextViewText(){

   MainActivity.text = "bulut";
}

Upvotes: 1

Views: 1351

Answers (3)

Sz-Nika Janos
Sz-Nika Janos

Reputation: 821

Prevent Memory Leaks and do not reference Contexts(Activities)

Interface callbacks method

Better Approach would be :

Make a interface:

interface TextUpdater{
void updateText(String text);
}

and implement it on your Activity, or create a instance of it

example:

Implementation method:

MainActivity extends... implements TextUpdater{
...
..


    @overRide 
  void updateText(String text){
   textView.setText(text);
}

Instance method:

TextUpdater textUpdater = new TextUpdater(){

    @overRide 
  void updateText(String text){
   textView.setText(text);
}
}

Finally add your interface instance to the other class via constructor or setter method.

ex1:  otherClass.setUpdater(this)  // (implementation case)
ex2:  otherClass otherclass = new OtherClass(this) 
 // if you make a instance of interface then use it (change 'this')

In your other class make a reference to interface:

 OtherClass {

  TextUpdater textUpdater...

 add constructor or do  setter...

and then call the interface like

textUpdater.updateText("Your String")

Notes: Make sure you run on UI Thread

LiveData method: (Better option)

OtherClass .. {



  MutableLiveData<String> textLiveData = new MutableLiveData()
     ...
     void changeText(String text){
      textLiveData.setValue(text)
     } 

Main Activity ...

otherClass.getTextLiveData().observe(this,Observer<String>() {
  @Override
  public void onChanged(final String text) {
    textView.setText(text)
  } 
  )}

Update your text like:

otherClass.changeText("Your text")

Upvotes: 0

Abdullah Tellioglu
Abdullah Tellioglu

Reputation: 1474

You should update your textview when you change your String. In your case, observer pattern is a good design pattern to accomplish this. Basic approach:

//create a class that stores object. 
public class Observer{
  private static Observer instance = new Observer();
  private HashMap<String,TextView> map;
  public static Observer getInstance(){
      return instance;
  }
  private Observer(){
    map = new HashMap<>();
  }
  public static void subscribe(String viewKey, TextView view){
    Observer observer = getInstance();
    observer.map.put(viewKey, view);  
  }
  public static void changeText(String viewKey, String text){
      Observer observer = getInstance();
      if(observer.map.containsKey(viewKey)){
          TextView textView = observer.map.get(viewKey);
          textView.setText(text);
      }else{
        // throw exception 
      }
  }

  // in your activity do this
  Observer.subscribe("Main text view", txt);

  // in your other classes simply do 
  Observer.changeText("Main text view", "bulut");

Of course you can use some libraries to accomplish this as well such as https://github.com/ReactiveX/RxAndroid . Or send your textview to your other classes as parameter of course

Upvotes: 2

Randyka Yudhistira
Randyka Yudhistira

Reputation: 3652

Its not recommended to use static in Activity class because it can cause memory leak. But if you must, then make the static in your TextView not your String. Something like :

public class MainActivity extends AppCompatActivity {

        public static String text = "123";
        Button btn;
        public static TextView txt;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
             txt = findViewById(R.id.txt);
             txt.setText(text);

        }
    }

Then in your another class

public class AnotherClass{  
 public void changeTextViewText(){

   MainActivity.txt.setText("bulut");
}

Recommended way

Make a constructor in your another class like :

public class AnotherClass{
  MainActivity mainActivity;
  public AnotherClass(MainActivity mainActivity) {
     this.mainActivity = mainActivity;
  }

  public void changeTextViewText() {
     mainActivity.getTxt().setText("bulut");
  }
}

And in your MainActivity : public class MainActivity extends AppCompatActivity {

    public static String text = "123";
    Button btn;
    TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         txt = findViewById(R.id.txt);
         txt.setText(text);

    }

    public TextView getTxt() {
        return txt;
    }
}

Upvotes: 1

Related Questions