Farukest
Farukest

Reputation: 1518

How can I show ProgressDialog at the start of the Activity?

I will open the Activity(name is VideoAcivity). This Activity is doing some video proccess and show the video. I know how to use progress dialog, Already I'm using progressdialog between my activities but i couldn't use progressdialog without AsyncTask, onPreExecute, onPostExecute. I want to show progressdialog when activity started without using AsyncTask because i'm using Thread in this Activity.

         super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_video);
     txtDisplay = (TextView) findViewById(R.id.textView1);
     getin = getIntent();
     video = getin.getStringExtra("Video_URL");
     subtitle = getin.getStringExtra("Subtitle_URL");
          mPreview = (SurfaceView)findViewById(R.id.surfaceView1);
     holder = mPreview.getHolder();
     holder.setFixedSize(800, 480);
     holder.addCallback(this);
     holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);       
     mediaPlayer = new MediaPlayer();
     mediaPlayer.setOnPreparedListener(this);
     mediaController = new MediaController(this);

       } // this is oncrete 
// ---> mediaplyer and mediacontroller codes..

public void onPrepared(MediaPlayer mediaPlayer) {
         Log.d(TAG, "onPrepared");
         mediaController.setMediaPlayer(this);
         mediaController.setAnchorView(findViewById(R.id.surfaceView1));

         handler.post(new Runnable() {
           public void run() {
             mediaController.setEnabled(true);
             mediaController.show();
           }
         });
       }

Thread th = new Thread(new Runnable() {

                @Override
                public void run() {
                // Codes..
        }
            });

         th.start();
         th.join();

I tried to add some codes and debugged, whatever i tried, ProgressDialog is not appearing when the activity start. I couldn't find the reason. It is appearing a little time at the end of the proccess. (All Proccess is taking 7-8 seconds, it is appearing only 1 second) Where am i doing wrong ? Where should I add the progress dialog codes? Hope you can help me..

Thanks in advance

Upvotes: 0

Views: 912

Answers (2)

antonyt
antonyt

Reputation: 21883

You are calling the Thread.join() method on your worker thread, seemingly from your main thread. This will make your main thread wait until your worker thread has completed. It won't be able to update the UI properly during this time. Android needs the main thread to be free to display the ProgressDialog.

What are you trying to accomplish with Thread.join() here? Try to remove it and see what happens.

Upvotes: 0

SachinGutte
SachinGutte

Reputation: 7055

Get progressDialog declared and put this in onCreate method-

progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loding");
progressDialog.show();
progressDialog.setCancelable(false);

And when your background task completes -

progressDialog.dismiss();

You need some kinda callback when your thread stops or you complete the task which you were doing in background. In asynctask you get onPostExecute method and it runs on main UI thread so you can dismiss progressDialog or do some other UI stuff. But in case of thread you don't have such method.

You need to identify when thread stops and then use this if you're still in thread-

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        progressDialog.dismiss();
    }
});

Upvotes: 1

Related Questions