0

Can I update two textviews inside a runnable? Because my code can only update one textview. I got this method that updates 2 textviews that contains the address and date from the EXIF data of a photo.

public void launchRingDialog() {

        final ProgressDialog ringProgressDialog = ProgressDialog.show(ReportIncident.this, "Please wait ...", "Rendering Image EXIF Data ...", true);

        ringProgressDialog.setCancelable(false);

        new Thread(new Runnable() {

            @Override

            public void run() {

                try {





                    Thread.sleep(5000);
                    loadExifData();
                    r.setDate(mExif.getAttribute(ExifInterface.TAG_DATETIME));
                    tvLocation.setText(getaddress());
                    tvTime.setText(r.getStringDate());
                    r.setLati(Double.parseDouble(mExif.getAttribute(ExifInterface.TAG_GPS_LATITUDE)));
                    r.setLongi(Double.parseDouble(mExif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE)));



                } catch (Exception e) {


                }

                ringProgressDialog.dismiss();

            }

        }).start();

    }
Aracem
  • 7,227
  • 4
  • 35
  • 72
Jaye
  • 132
  • 1
  • 10

1 Answers1

1

You cannot update the UI in a different Thread than the UI Thread in Android. To do that, a simple way you can use is:

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
    tvLocation.setText(getaddress());
    tvTime.setText(r.getStringDate());
});
  • runOnUiThread method is particular to an Activity and can be seen as a special case of the more generic solution which I provided above. Using handlers allow you to update your UI even if you are not in the Activity class. – Tomás Ruiz-López Jul 17 '15 at 16:25
  • Your code worked but I have to put the two setTexts inside a public void run() and my ringProgressDialog is now gone. – Jaye Jul 18 '15 at 02:03