title: How to stop the UI thread in my app until other thread completes printing
body: <p>Description: </p>
  
  <ul>
  <li>In my app, I need to print data using Bluetooth printer. </li>
  <li>The UI thread has to wait until the other thread dealing with printing
  completes its task</li>
  <li>The printing thread sets a value whether it is able to print or not</li>
  </ul>
  
  <p>problem: The UI thread in the app is not waiting and its immediately getting the default status false</p>
  
  <p>Where I am doing mistake in the following code?</p>
  
  <p>UI thread:</p>
  
  <pre><code>private void printingStatus()
  {
      if(printData.isPrintFinished())
          // do something
      else
          // do something
  }
  </code></pre>
  
  <p>// PrintData class that executes printing in a different thread</p>
  
  <pre><code>    private boolean printFinished = false;
  // UI thread asks this method whether printing is success or failure
  public boolean isPrintFinished() {
      return printed;
  }
  
  public void setPrintFinished(boolean printed) {
      this.printed = printed;
  }
  
  public static interface IPrintFinished {
      public void onPrintFinished(boolean success);
  }
  
  public void printData() {
      sendDataToPrinter(context, contentToPrint, new IPrintFinished() {
          @Override
          public void setPrintFinished(boolean success) {
              if (success) {
                  setPrintFinished(true);
                  Logger.e("inside if success - printed"+printed);
              }
              setPrintFinished(false);
              Logger.e("printed outside if" + printed);
          }
      });
  }
  public static void sendDataToPrinter(final Context context, final contentDefinition contentToPrint, final IPrintFinished listener) {
      new Thread(new Runnable() {
      @Override
      public void run() {
          Looper.prepare();
          // starts here the actual printing and gets a result
          int tmpResult;
            try {
                DataPrinter.getInstance().print(contentToPrint);
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    Logger.e("exceptpion:" + e);
                }
                tmpResult =  STATUS_PRINTED;
            } catch (TechnicalException e) {
                Logger.e("exceptpion:" + e);
                if (e.getMessage() != null &amp;&amp; DataPrinter.ERROR_BLUETOOH_IS_DISABLED.equals(e.getMessage()))
                    tmpResult = STATUS_BLUETOOTH_DISABLED;
                else
                    tmpResult = STATUS_ERROR;
            }
  
  
            final int result = tmpResult;
            Logger.e("print result:"+result);
            new Handler(Looper.getMainLooper()).post(new Runnable() {
  
                @Override
                public void run() {
  
                    if(result == STATUS_PRINTED) {
                        if (listener != null) {
                            Logger.e("inside the listener status printed");
                            listener.onPrintFinished(true);
                        }
                    } else if (result == STATUS_BLUETOOTH_DISABLED) {
                        SimpleToast.showToast("bluetooth disabled");
                        if (listener != null) {
                            listener.onPrintFinished(false);
                        }
                    } else {
                        SimpleToast.showToast("cannot connect to printer");
                        if (listener != null) {
                            listener.onPrintFinished(false);
                        }
                    }
                }
            });
  
                  Looper.loop();
              }
          }).start();
      }
  </code></pre>