you only need to run the code in a thread and call Thread.sleap
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView2);
final String[] originalText = {""};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread() {
@Override
public void run() {
originalText[0] = textView.getText().toString();
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("what ever!!");
}
});
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(originalText[0]);
}
});
}
}.start();
}
});
by using lambda the code will be much more concise
Button button = (Button) findViewById(R.id.button);
final TextView textView = (TextView) findViewById(R.id.textView2);
final String[] originalText = {""};
button.setOnClickListener(view -> {
new Thread() {
@Override
public void run() {
originalText[0] = textView.getText().toString();
runOnUiThread(() -> textView.setText("what ever!!"));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(() -> textView.setText(originalText[0]));
}
}.start();
});