Last active
July 15, 2016 09:59
-
-
Save aldakur/1d358dc07f198a040c0639343102a5e3 to your computer and use it in GitHub Desktop.
Thread that changes the user interface in the same (primary) thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [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