-2

I've tried using Thread, but it was unsuccessful, the textView didn't changed after I change the TextView text using EditText as input. Help me, please!

        Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            //shared is SharedPreferences object that I define as an instance variable
            String inp = shared.getString("input", def);

            textView.setText(inp);
            Log.d("input",inp);
    });

    thread.start();

2 Answers2

0

Why are you doing it in a separate thread. Even if you want to, you cannot update the any UI component such as textView in a non UI thread.

Arpit Ratan
  • 2,837
  • 1
  • 10
  • 20
0

try this:

    Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        //shared is SharedPreferences object that I define as an instance variable
        String inp = shared.getString("input", def);
         runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        textView.setText(inp);
                        Log.d("input",inp);
                    }
                });
});

thread.start();

for updating UI you should use the main ui thread. take a look at :

Android runOnUiThread explanation

Community
  • 1
  • 1
pouyan
  • 3,192
  • 2
  • 21
  • 38