Fill up table layout and show progress dialog
<p>I am filling up table layout via program. It is taking to much time.
So till the whole tablelayout don't filled up, I want to show progress dialog.<br>
I cannot use new thread other than UIthread to show progress dialog as well as filling up table layout. </p>

<p>How can I show porgress dialog at the same time while filling up table layout.</p>

<p>Thanks.</p>

<p>Here is whole code</p>

<pre><code>public class MainActivity extends Activity {

    private static TextView edtSearch;
    private ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ArrayList&lt;String&gt; lsMdlMaterial  = new ArrayList&lt;String&gt;();
        for(int i=0;i&lt;1000;i++){
            lsMdlMaterial.add("item:"+(i+1));
        }
        final TextView Txt= new TextView(this);
        final Handler handler= new Handler();
        final Handler handler1= new Handler();
        showProgressDialog();
        new Thread(new Runnable() {

            @Override
            public void run() {




                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        showCommoditySelection(MainActivity.this,Txt, lsMdlMaterial);
                        dismissProgressDialog();
                    }
                });


            }
        }).start();

    }

    private void showProgressDialog() {
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog = ProgressDialog.show(
                this,
                "",
                "loading", true);
    }

    /**
     * if progressdialog not null than dismiss.
     * **/
    private void dismissProgressDialog() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    // showCommoditySelection Selection using Interface.
    public static void showCommoditySelection(final Activity activity,

            final TextView txtMaterial,
            final ArrayList&lt;String&gt; lsMdlMaterial) {
        /* click distributor view and open popup */

        final Dialog popupWindow = new Dialog(activity);
        popupWindow.requestWindowFeature(Window.FEATURE_NO_TITLE);
        popupWindow.setContentView(R.layout.xml_sc_selectdistributor);
        popupWindow.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        popupWindow.setCanceledOnTouchOutside(false);
        popupWindow.show();

        final TableLayout SortByTableView = (TableLayout) popupWindow
                .findViewById(R.id.xml_sc_selectdistributor_tblView);
        edtSearch = (EditText) popupWindow
                .findViewById(R.id.xml_salescontract_edtSearch);


        /*edtSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence keyword, int start,
                    int before, int count) {
                FillCommodity(popupWindow, activity,
                        txtMaterial, lsMdlMaterial, SortByTableView,
                        keyword.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence keyword, int start,
                    int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });*/
        FillCommodity(popupWindow, activity, txtMaterial,
                lsMdlMaterial, SortByTableView, "");

    }

    private static void FillCommodity(final Dialog popupWindow,
             final Activity activity,
            final TextView txtMaterial,
            final ArrayList&lt;String&gt; lsMdlMaterial,
            TableLayout SortByTableView, String keyword) {
        SortByTableView.removeAllViews();
        for (int i = 0; i &lt; lsMdlMaterial.size(); i++) {
            String codeName = lsMdlMaterial.get(i)
                    .toLowerCase()
                    ;
            if (codeName.contains(keyword)) {
                View viewLine = new View(activity);

                viewLine.setLayoutParams(new LayoutParams(514, 1));
                final TextView txt = new TextView(activity);
                txt.setHeight(40);

                txt.setPadding(10, 10, 10, 10);
                txt.setTextSize(16);
                txt.setId(i);
                txt.setText(lsMdlMaterial.get(i));
                SortByTableView.addView(txt);
                SortByTableView.addView(viewLine);
                txt.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {


                        popupWindow.dismiss();

                    }
                });
            }
        }

    }

}
</code></pre>
