Search code examples
androidtextviewrunnableandroid-runonuithread

Update two TextViews inside a runnable


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();

    }

Solution

  • 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());
    });