1

I am adding views dynamically to linear layout in a for loop of more than 100 loops.

Now what I want to show ProgressDialog while adding views. I added progressdialogue but it dismisses before views actually appear on the screen.

Is there any callback for views visible on screen

Here is my code

final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Please wait...");
pd.setCancelable(false);
pd.show();

for (int i = 0; i < rowItemsListTemp.size(); i++) {
    View view;
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.product_item_card, null);

    txt_pro_name = (TextView) view.findViewById(R.id.txt_pro_name);
    txt_pro_desc = (TextView) view.findViewById(R.id.txt_pro_desc);

    txt_pro_name.setText(rowItemsListTemp.get(i).getProName());
    txt_pro_desc.setText(Html.fromHtml(rowItemsListTemp.get(i).getProDesc()));
    ll_batcheslist.addView(view);
}

if (pd.isShowing()) {
    pd.dismiss();
}
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • I recommend you to use `RecyclerView` or `ListView`. – mr.icetea Jul 22 '16 at 08:32
  • I have more views and logic inside for loop which I have not posted here, I cannot use use recyclerview or listview as per my requirement. I know this is not good approach. – Shreenivas Chikati Jul 22 '16 at 09:04
  • Possible duplicate of [ProgressDialog not showing while performing a task](http://stackoverflow.com/questions/12897205/progressdialog-not-showing-while-performing-a-task) – Sufian Jul 24 '16 at 11:14

2 Answers2

0

You can try some other logic too, but one I can suggest is:

 for (int i = 0; i =< rowItemsListTemp.size(); i++) {

   if(i=rowItemsListTemp.size()){
      if (pd.isShowing()) {
         pd.dismiss();
      }
   }else{
    View view;
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.product_item_card, null);

    txt_pro_name = (TextView) view.findViewById(R.id.txt_pro_name);
    txt_pro_desc = (TextView) view.findViewById(R.id.txt_pro_desc);

    txt_pro_name.setText(rowItemsListTemp.get(i).getProName());
    txt_pro_desc.setText(Html.fromHtml(rowItemsListTemp.get(i).getProDesc()));
    ll_batcheslist.addView(view);
   }

}
Dhruvi
  • 1,971
  • 2
  • 10
  • 18
0

Put this at the end of your for loop,

if(i == rowItemsListTemp.size()-1){
       if (pd.isShowing()) {
          pd.dismiss();
       }
    }

However, this does not seem like a good implementation.

... in a for loop of more than 100 loops.

Tony
  • 2,242
  • 22
  • 33