title: Android: Loading Dialog for Activity
body: <p>I'm trying to implement the code from <a href="https://stackoverflow.com/questions/2645271/android-showing-dialog-while-loading-layout-by-setcontentview-in-background">showing dialog while loading layout by setContentView in background</a> and <a href="http://developer.android.com/guide/appendix/faq/commontasks.html#threading" rel="nofollow noreferrer">http://developer.android.com/guide/appendix/faq/commontasks.html#threading</a> to show a loading dialog while my activity is loading, but having difficulty.</p>
  
  <p>I have class variables defined for the UI elements in my view, and also strings for the data which is loaded on another thread from the database:</p>
  
  <pre><code>private TextView mLblName, mLblDescription, etc...
  private String mData_RecipeName, mData_Description...
  </code></pre>
  
  <p>I also have the handlers defined:</p>
  
  <pre><code>private ProgressDialog dialog;
  final Handler mHandler = new Handler();
  final Runnable mShowRecipe = new Runnable() {
      public void run() {
          //setContentView(R.layout.recipe_view);
      setTitle(mData_RecipeName);
      mLblName.setText(mData_RecipeName);
      mLblDescription.setText(mData_Description);
      ...
      }
  };
  </code></pre>
  
  <p>In onCreate, I'm trying too show the dialog, then spawn the loading thread:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
      setContentView(R.layout.recipe_view);
      showData();
  }
  
  protected void showData() {
      // Fire off a thread to do some work that we shouldn't do directly in the UI thread
      Thread t = new Thread() {
          public void run() {
              mDatabaseAdapter = new ChickenPingDatabase(ShowRecipe.this);
          mDatabaseAdapter.open();
  
          mTabHost = getTabHost();
  
          mLblName = (TextView)findViewById(R.id.lblName);
          mLblDescription = (TextView)findViewById(R.id.lblDescription);
          ...
  
          Cursor c = mDatabaseAdapter.getRecipeById(mRecipeId);
          if(c != null){
              mData_RecipeName= c.getString(c.getColumnIndex(Recipes.NAME));
                      mData_Description= c.getString(c.getColumnIndex(Recipes.DESCRIPTION));
              ...
                      c.close();
          }
  
          String[] categories = mDatabaseAdapter.getRecipeCategories(mRecipeId);
          mData_CategoriesDesc = Utils.implode(categories, ",");
  
          mHandler.post(mShowRecipe);
  
          }
      };
      t.start();
  }
  </code></pre>
  
  <p>This loads the data, but the progress dialog isn't shown. I've tried shuffling the call to spawn the separate thread and show the dialog around, but can't get the dialog to show. It seems this is a fairly common request, and this post seemed to be the only answered example of it.</p>
  
  <p><strong>EDIT:</strong> For reference, <a href="http://www.leghumped.com/blog/2011/02/27/android-loading-dialog-for-long-running-tasks/" rel="nofollow noreferrer">a blog post demonstrating the way I eventually got this working</a>.</p>