There's a button in my program that, when clicked, accesses a huge database and takes a second or two to do that, and then disappeaars. During that waiting time, I would like the button's text to change to "LOADING..." or something. I tried
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
myButton.setText("LOADING...");
//then do other stuff
}
but of course lines of code aren't executed sequentially like that, so the text doesn't show up (or, shows up so quickly before disappearing that it isn't noticeable). Is there a simple way to do this? The only thing that comes to mind is using a Timer but (1) I'm not really sure how and (2) that seems overly complicated for just one simple line of code like that.
The problem is that everything is done on the UI thread, and the long database access blocks the UI thread and prevents the "Loading..." text to be displayed. You should perform the database operation outside of the UI thread. Read http://developer.android.com/resources/articles/painless-threading.html.