Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Thread that changes the user interface in the same (primary) thread
// [EU] Hari berdinean (app-aren hari nagusia) interfaze aldaketa burutzeko. Kasu honetan
// TextView baten aldaketa
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView texto;
Button boton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
texto = (TextView) findViewById(R.id.texto);
boton = (Button) findViewById(R.id.boton);
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
final String txt = "This is a text";
// this is a way using post
texto.post(new Runnable() {
@Override
public void run() {
texto.setText(txt);
}
});
// this is another way using runOnUiThread instead post
/*
runOnUiThread(new Runnable() {
@Override
public void run() {
texto.setText(txt);
}
});*/
}
}).start();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.