progress dialog in main activity's onCreate not shown - android

After the splash screen, it takes about 6 sec to load onCreate contents in the Main activity. So I want to show a progress dialog while loading and here's what I did:
import ...
private ProgressDialog mainProgress;
public void onCreate(Bundle davedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mProgress = new ProgressDialog (Main.this);
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setMessage("Loading... please wait");
mProgress.setIndeterminate(false);
mProgress.setMax(100);
mProgress.setProgress(0);
mProgress.show();
---some code---
mProgress.setProgress(50);
---some code---
mProgress.setProgress(100);
mProgress.dismiss();
}
and it doesn't work... the screen stays black for 5-6 sec and then load the main layout. I dont know which part I did wrong :*(

You need to do start your busy code in an other thread (than the UI thread).
In your activity create the ProgressDialog and close it in the thread
mProgress = new ProgressDialog (Main.this);
Runnable runnable = new Runnable() {
#Override
public void run() {
// code that needs 6 seconds for execution
// after finishing, close the progress bar
mProgress.dismiss();
}
};
new Thread(runnable).start();

It's because you're doing everything on the UI thread, thus blocking it.
You should do all your heavy-lifting in a separate thread and update the progress dialog through that

Related

Android progress dialog while waiting to dialog show up

In my android application, dialog's showing up takes a time. At that time I want to show progress dialog. I learned that the progress dialog should be executed in thread but when I use a thread it gives an error.
I created progress dialog in oncreate method and tried to show in my button's onclick method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
mProgressDialog = new ProgressDialog(context, R.style.StyledDialog);
mProgressDialog.setCanceledOnTouchOutside(false);
Drawable drawable = context.getResources().getDrawable(R.drawable.progress_dialog);
mProgressDialog.setProgressDrawable(drawable);
}
Following code is my button's onclick method which is defined in xml file like : android:onClick="refList"
public void refList(View v) {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
mProgressDialog.show();
}
});
t.start();
if(!refListDialog.isShowing()) {
refListDialog.show();
t.interrupt();
}
}
This is the exception:
FATAL EXCEPTION: Thread-27305 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.view.ViewRootImpl$ViewRootHandler.<init>(ViewRootImpl.java:3052)
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:3321)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:294)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:226)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:151)
at android.app.Dialog.show(Dialog.java:277)
at gcm.b4deploy.com.hesapozeti.MainActivity$2.run(MainActivity.java:196)
at java.lang.Thread.run(Thread.java:856)
I am really stuck and waiting ideas. Thanks in advance.
I really don't get the idea why you put the progressDialog inside a thread.
yourProgress = new ProgressDialog(this);
yourProgress.setTitle("Title");
yourProgress.setMessage("wait for a while"); yourProgress.getProcess();
yourProgress.show();
Put this code first in your function in the onClick method. It will execute sequentially.And you can make conditional statement, and if done, call yourProgress.dismiss();
- add your statement above the progressDialog.setTitle. I suggest you declare the progress dialog outside this method, so that you can dismiss that one in the other method which return true. Just remove those runnable thing.
Try like this
runOnUiThread(new Runnable() {
public void run() {
mProgressDialog.show();
}
});

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
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();
}
});
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.

Android: ProgressDialog not showing immediately

I have a problem of showing the progressDialog on android. It did shows up to the screen but it took few seconds before it really show the dialog.
This is the code i did to show the dialog
Handler saveHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
GallerySaveActivity.this.Submit(progress);
Button btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setEnabled(true);
}
};
progress.showDialog(saveHandler, "", "Accessing Facebook ...");
Thread progress_thread = new Thread(progress);
progress_thread.start();
Do I have to do any extra work on the Thread object in order to show the dialog instantly without any delay.
Consider using an AsyncTask: show your dialog in onPreExecute(), and do your background tasks in doInBackground().

Using ProgressDialog in android application startup

How can I show a progressDialog during the start up of an application. I have shown a progressDialog in the oncreate method and its not showing when launching the application.
I have gone through this:
ProgressDialog not showing until after function finishes
I have tried the solution explained for the above question. But its not working perfectly.
Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dash);
progressDialog = ProgressDialog.show(this, "", "Loading...");
//Run background UI thread
Thread laucherThread = new Thread(new Runnable() {
public void run() {
Looper.prepare(); //I had to include this to prevent force close error
startService(new Intent(DroidChirp.this,ChirpService.class));
doBindService();
Log.e(MY_DEBUG_TAG, "Prepairing to close the dialog");
runOnUiThread(new Runnable() {
public void run() {
progressDialog.dismiss();
}
});
Log.e(MY_DEBUG_TAG, "Closed the dialog");
}
});
laucherThread.start();
}
What I need to do is:
Show a progressDialog until all the initial setup is finished
Issues I am facing :
ProgressDialog is not showing at the start up.
There is a delay when starting the application(Showing blank for sometime).
ProgressDialog appears just before finishing the initial setup.
Can anyone suggest me how can I establish this feature. Its a tablelayout with list view for each tab.
If you want to show a progress dialog, while fetching data or something like this, you need to use AsyncTask with ProgressDialog bounded. See an example here.

Progress Dialog

-->I am new to Android And i want to show two progress dialog one after another??
-->First i want to show when my image is load from internet, when this process is done i have set A button on that Remote image.
-->When i click that button i want Dialog for second time..(on clicking button i have set video streaming code.. before video is start i want to close that Dialog..)
Any Help????
Thanks...
You can create 2 threads. First for image when it is loaded then call another thread of video.
Make two runnable actions and two handlers to handle that
public void onCreate(Bundle savedInstanceState) {
ProgressDialog pd = ProgressDialog.show(this, "","Please Wait", true, false);
Thread th = new Thread(setImage);
th.start();
}
public Runnable setImage = new Runnable() {
public void run() {
//your code
handler.sendEmptyMessage(0);
}
};
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (pd != null)
pd.dismiss();
}
};
On Android it's best to use AsyncTask to execute tasks in the background while still updating UI:
Extend the AsyncTask
Start the progress dialog in onPreExecute()
Define the background task in doInBackground(Params...)
Define updating of the progress dialog in onProgressUpdate(Progress...)
Update dialog by calling publishProgress() from doInBackground()
Start a new Dialog #2 in onPostExecute().

Categories

Resources