Reputation: 7975
Hi I have this piece of code:
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
morseText = editText.getText().toString();
message.setText("Sending Message...");
message.setTextColor(Color.BLACK);
sendMessage();
return handled;
}
});
Everything works as expected except that setText and setTextColor are taking effect after sendMessage() has already finished, and for the record sendMessage() might take a few seconds. I need the text to change before sendMessage.
How can I solve this and why this is happening? Thank you
Upvotes: 0
Views: 831
Reputation: 32221
Delay the sendMessage
after the UI been given a chance to draw your changes. Also if sendMessage
takes seconds of time, consider putting it on IO thread.
TextView editText = null;
assert editText != null;
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
morseText = editText.getText().toString();
message.setText("Sending Message...");
message.setTextColor(Color.BLACK);
v.postDelayed(new Runnable() {
@Override
public void run() {
sendMessage();
}
}, 1);
return handled;
}
});
Upvotes: 0
Reputation: 481
try this code to run sendMessage():
new Handler().post(new Runnable(){
public void run(){
sendMessage();
}
});
or use postDelay to replace post.
Upvotes: 0
Reputation: 1006799
setText and setTextColor are taking effect after sendMessage() has already finished
They will not take effect until you return control of the main application thread to the framework, when onEditorAction()
returns.
for the record sendMessage() might take a few seconds
Then that work has to be done on a background thread.
How can I solve this
Move any work that will take longer than ~1ms off of the main application thread and onto background threads. You may wish to read more about Android's threading model.
Upvotes: 2