I’m trying to make a TextView blink while the user is in the Main Activity.
I created a new thread but what happens is that the TextView’s visibility turns on, than off and then the app closes itself with Android saying that the app stopped working.
Seems like the for loop executes ones and then the whole application is interrupted.
Here’s a code snippet
Thread blinkThread = new Thread(){
@Override
public void run() {
for(int i = 0; i < 10; i++){
lastletter.setVisibility(VISIBLE);
try {
sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
lastletter.setVisibility(View.INVISIBLE);
try {
sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
};
The code above code is inside the MainActivity class.
The thread starts in the onCreate() method, I basically wrote blinkThread.start()
2
Answers
Try to use below code:
Code:
In android, manipulating views from other threads than the main thread throws an exception.
You need to make a handler for the main thread.
and post runnables to it.