Skip to content

Instantly share code, notes, and snippets.

@aldakur
Last active July 15, 2016 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldakur/1d358dc07f198a040c0639343102a5e3 to your computer and use it in GitHub Desktop.
Save aldakur/1d358dc07f198a040c0639343102a5e3 to your computer and use it in GitHub Desktop.
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