As a beginner in android programming, I am taught that can't change UI view components from non-ui thread, but in the following simple code snippet it seems that it can be done(non ui thread changes button text which is created by UI thread)... I've done research and I should get CalledFromWrongThreadException. Please help?
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
}
public void clickDo(View view){
MyRunnable myRunnable = new MyRunnable();
new Thread(myRunnable).start();
}
class MyRunnable implements Runnable{
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
button.setText("newText"); //<-------- here?
}
}
}