Bart Simons

ui code

A 1 post collection

Android - Call UI-code from a class

 •  Filed under android, ui code, class, tutorial, example

Java and the Android SDK go perfectly well together, but just like any other 'good' programming language you sometimes have to get over some rough edges. As an example, it took me almost a day to figure out how to interact with the UI thread from a class! So, I'm sharing with you this handy little Java snippet that just works. Enjoy it and stay productive!


package me.bartsimons.helloworld

import android.os.Bundle;  
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {  
    TextView helloText;

    protected void onCreate(Bundle savedBundleInstance) {

        helloText = (TextView) findViewById(;

        SubModule moduleSub = new SubModule(this);

package me.bartsimons.helloworld

public class SubModule {  
    protected MainActivity context;

    public SubModule(MainActivity _context) {
        context = _context;

    public void testFunction() {
        context.runOnUiThread(new Runnable() {
            context.helloText.setText("Hello world!");