I have a TextView
whose layout_width
is set to wrap_content
. I use a nine-patch image as background. TextView's content is periodically refreshed (I use thread which sleeps some time and then starts a handler). After few iterations there are some artifacts. I can see a part of text and background of some earlier message (it's width is bigger then the width of currently shown message). What could be the problem?
public void onCreate{
...
helpField = (TextView) findViewById(R.id.helpField);
...
}
private class PeriodicHelp extends Thread{
private static final int SLEEP_TIME = 4000;
private static final int NUM_HELP_PHRASES = 5;
private String getHelp(){
int pos = randomGenerator.nextInt(NUM_HELP_PHRASES);
return helpPhrases[pos];
}
public void run(){
int i = 0;
while(true){
try {
Thread.sleep(SLEEP_TIME);
Log.d(TAG, "periodicHelp " + Integer.toString(i));
i++; //used just for debugging
mHandler.post(new Runnable(){
public void run() {
String help = getHelp();
helpField.setText(help);
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
I start my PeriodicHelp thread in onStart()
I manged to solve this problem. Here is the code
public void onCreate{
...
helpField = (TextView) findViewById(R.id.helpField);
rl = (RelativeLayout) findViewById(R.id.mainView); //this RelativeLayout contains
//helpField TextView
...
}
private class PeriodicHelp extends Thread{
private static final int SLEEP_TIME = 4000;
private static final int NUM_HELP_PHRASES = 5;
private String getHelp(){
int pos = randomGenerator.nextInt(NUM_HELP_PHRASES);
return helpPhrases[pos];
}
public void run(){
int i = 0;
while(true){
try {
Thread.sleep(SLEEP_TIME);
Log.d(TAG, "periodicHelp " + Integer.toString(i));
i++; //used just for debugging
rl.postInvalidate();//THIS LINE FIX THE PROBLEM
mHandler.post(new Runnable(){
public void run() {
String help = getHelp();
helpField.setText(help);
}
});
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}