1

Here in may application i have implement custom Progress bar but custom progress bar

1st Issue: 1st issue is that Progress Bar just showing and not animating, i have use asynctask for loading heavy data from external database in Background.

2nd Issue: The 2nd one problem is that when i click on link to load heavy data then it switch on another activity that show the loaded result.The problem is that when i press a link for load data it take 7-8 sec, and showing a black screen for 3-4 sec when switching another screen how can i avoid to showing black screen?

Mean when i press link for load data it take 3-4 SEC, on(1st screen) current screen, then appear black screen for 3-4 sec then show (2nd screen) for result, i don't know why it is happening, and how i can avoid it. I want when i click on link then one progress bar should be execute and goes secondActivity,without showing black screen. please where i should use progress dialog on 1st screen(current screen) or second screen? or on both screen? please any help will be appreciated.

Here is my Custom adapter

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{

    Holder holder=new Holder();
    View rowView;
    rowView=inflater.inflate(R.layout.nav_navitem,null);
    holder.tv=(TextView)rowView.findViewById(R.id.tvlstName);

    holder.tv.setTypeface(face);
    holder.img=(ImageView)rowView.findViewById(R.id.img);
    holder.tv.setText(result[position]);

    holder.img.setImageResource(imageId[position]);
   rowView.setOnClickListener(new View.OnClickListener() {
                                   @Override
                                   public void onClick(View v) {
                                       //Toast.makeText(context,"you Clicked "+result[position],Toast.LENGTH_LONG).show();
                                       if(result[position]=="کھانوں کی فہرست".trim().toString())
                                       {

                                           new AsyncTask<Void, Void, String>() {
                                               @ Override
                                               protected void onPreExecute ( ) {
                                                   //for progress bar
                                                   progressDialog = new ProgressDialog(context);
                                                   progressDialog.show ( ) ;
                                                   progressDialog.setContentView(R.layout.custom_progressdialog);
                                                   progressDialog.setCancelable(true);

                                               }
                                               @Override
                                               protected String doInBackground(Void... params) {

                                                           //stuff that updates ui
                                                           context.startActivity(new Intent(context, SecondActivity.class));



                                                   return null ;
                                               }

                                               @Override
                                               protected void onPostExecute(String msg) {
                                                   if(progressDialog != null && progressDialog.isShowing()) {
                                                       progressDialog.dismiss();
                                                   }
                                               }


                                           }.execute(null, null, null);

                                       }

When screen switch SecondActivity.class then show black screen.

Here is the SecondActivity class some relevant code.

public class SecondActivity extends AppCompatActivity {
private SQLiteDatabase sqLiteDatabase;
public static Typeface face;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    face = Typeface.createFromAsset(getAssets(), "font/asunaskh.ttf");



    Toolbar toolbar=(Toolbar)findViewById(R.id.sectoolbar);
   setSupportActionBar(toolbar);
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

   CollapsingToolbarLayout collapsingToolbarLayout=(CollapsingToolbarLayout)
         findViewById(R.id.collapsing_toolbar);
   collapsingToolbarLayout.setTitle("کھانے کی فہرست");
    collapsingToolbarLayout.setCollapsedTitleTypeface(face);
    collapsingToolbarLayout.setExpandedTitleTypeface(face);


    progressDialog = new ProgressDialog(SecondActivity.this);
    progressDialog.setMessage("Loading");
    progressDialog.setCancelable(true);


    new AsyncTask<Void, Void, String>() {
        @ Override
        protected void onPreExecute ( ) {
            progressDialog.show ( ) ;
        }
        @Override
        protected String doInBackground(Void... params) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //stuff that updates ui
                    show_card();
                                        }
            });

            return null ;
        }

        @Override
        protected void onPostExecute(String msg) {
            if(progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }


    }.execute(null, null, null);

}
//show_card() method goes here
public void show_card(){....}

This is a custom progress bar that show before switch SecondActivity when i click to a link Custom Progress Bar

Here is 2nd screen shot(black screen) of Progress BAr when activity switch form current to another activity. Progress Bar Screen Shot

Bella
  • 33
  • 5

1 Answers1

0

1st Issue: You will need to implement onProgressUpdate() in your AsyncTask to animate your progress bar. See documentation for AsyncTask.

2nd Issue: I think the issue here is that you are starting the second activity from the AsyncTask's doInBackground which does not run on the UI thread. (Activities should run on the UI thread.) onPreExecute and onPostExecute both run on the UI thread, so you might move the start of the second activity there. If the only thing the AsyncTask is doing is to start the Activity then you don't need the AsyncTask at all - just start the Activity.

Take a look at this SO question and the accepted answer for some guidance.

Community
  • 1
  • 1
Cheticamp
  • 61,413
  • 10
  • 78
  • 131