0

I'm trying to change some textview value but when it's not appeared on the screen the value don't change or when it's appeared on screen it changes and when scroll down and scroll back up it's value returns to the old one i tried the following two ways but non of them is working :

final Timer timer=new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                myTextView.setText("sometext");
                timer.cancel();
            }
        });
    }
}, 5000, 1000);

and:

MainActivity.this.r1.post(new Runnable() {
    public void run() {
        myTextView.setText("sometext");
    }
});

2 Answers2

0

Try using Handler

    private void TestThread() {
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {
                 handler.post(new Runnable(){
                    public void run() {
                       tvTime.setText("SomeText");
                    }
                 });
        }
    }
    };
    new Thread(runnable).start();
}

Also you can take a look AsyncTask Documentation and onPostExecute() you set the text to TextView.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

You have to make sure that the UI is drawn first before you change the value of the TextView , It can be done by adding a layout listener,

ViewTreeObserver vto = getWindow().getDecorView().getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Overrid
public void onGlobalLayout() {
final Timer timer=new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        MainActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                myTextView.setText("sometext");
                timer.cancel();
            }
        });
    }
}, 5000, 1000);
}
}
Parag Kadam
  • 3,620
  • 5
  • 25
  • 51