Issues In Splash Screen with progressDialog - java

My Issue is progress Dialog not display with the splash Screen?Can Any one solve this, Any help could be appreciated thanks in advance !
public class Splash extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
Thread thread = new Thread(){
#Override
public void run() {
try
{
sleep(3*1000);
ProgressDialog progressDialog = new ProgressDialog(Splash.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("wait");
progressDialog.setCancelable(false);
progressDialog.show();
}catch (Exception e)
{
e.printStackTrace();
}finally {
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
finish();
}
}
};thread.start();
}
}

public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
ProgressDialog progressDialog = new ProgressDialog(Splash.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("wait");
progressDialog.setCancelable(false);
progressDialog.show();
Thread thread = new Thread(){
#Override
public void run() {
try
{
sleep(3*1000);
}catch (Exception e)
{
e.printStackTrace();
}finally {
progressDialog.dismiss();
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
finish();
}
}
};thread.start();
}
}

Cuz you are updating the UI in background thread..
try to use the
runOnUiThread(new Runnable.......)
or try to put the UI work on UI thread.

Everthing you write inside a thread will be executed in background. you can't manipulate any UI elements from a background Thread. you should be getting an error from this code, check your stacktrace. I suggest you remove the code for the ProgressDialog from the Thread and put it before the Thread.

You should show the progress dialog from UI thread. Or you can use runOnUiThread(...) method. If you have to show it from a different thread write thin inside run method of thread:
Handler mainHandler = new Handler(Looper.getMainLooper());
mainHandler.post(new Runnable() {
#Override
public void run() {
//add try catch
sleep(3*1000);
ProgressDialog progressDialog = new ProgressDialog(Splash.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
});
I would suggest using handlers instead of sleep in your activity. You can also try this without the thread in your code:
Handler h = new Handler(Looper.getMainLooper())
h.postDelayed( new Runnable() {
#Override
public void run() {
ProgressDialog progressDialog = new ProgressDialog(Splash.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
},
(3*1000));

Related

Android dialog dismiss after certain computation

I'm encountering the following problem:
I created a waiting spinner using Dialog like this
final Dialog progDialog = new Dialog(context);
progDialog.setContentView(R.layout.progress_dialog);
progDialog.setTitle("Calculating...");
progDialog.setCancelable(false);
progDialog.setCanceledOnTouchOutside(false);
WindowManager.LayoutParams lp = progDialog.getWindow().getAttributes();
lp.dimAmount = 0.7f;
progDialog.show();
Afterwards, I'm calculating something in the background
for()...
for()...
After the calculation is finished, I want to dismiss my dialog with
progDialog.dismiss();
However, this results in my dialog never being shown at all. When I remove the last line, the dialog is shown but is never dismissed. Is there a fix to it?
You better try with AsyncTask
private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progDialog;
public YourAsyncTask(MyMainActivity activity) {
progDialog = new ProgressDialog(activity);
}
#Override
protected void onPreExecute() {
progDialog.setContentView(R.layout.progress_dialog);
progDialog.setTitle("Calculating...");
progDialog.setCancelable(false);
progDialog.setCanceledOnTouchOutside(false);
progDialog.show();
}
#Override
protected Void doInBackground(Void... args) {
// do background work here
return null;
}
#Override
protected void onPostExecute(Void result) {
// do UI work here
if (progDialog.isShowing()) {
progDialog.dismiss();
}
}
}
Use the above code in your Main Activity. And, do your calculation part in doInBackground.
To set timer for your computation try below code:
Runnable progressRunnable = new Runnable() {
#Override
public void run() {
progDialog.cancel();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(progressRunnable, 3000);
Adding show/hide:
progDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
theLayout.setVisibility(View.GONE);
}
});
Update:
ProgressDialog class was deprecated as of API 26

I want Android progress dialog to stay on screen until function on new activity is complete but doesn't work

In my Android app, one form has a button which upon click opens up another; the new form performs activities which can take a while. I want the first form to remain open and for the progress dialog to keep spinning while these activities finish.
I've attempted this below, but it just won't work. The progress dialog just finishes and opens up the next window (before described activities on new form have finished)
In the below code SecondForm -
The subroutine "Calculations", is what takes a while to complete
Code:
MainActivity:
final ProgressDialog ringProgressDialog = new ProgressDialog(
MainActivity.this);
ringProgressDialog.setTitle("Loading");
ringProgressDialog.show();
ringProgressDialog.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(
getApplicationContext(),
SecondForm.class);
startActivity(intent);
}
});
} catch (Exception e) {
}
ringProgressDialog.dismiss();
}
}).start();
SecondForm:
public class CategoryTabs extends Fragment {
static Context mContext;
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
gotstatdata = false;
rootView = inflater.inflate(R.layout.fragment_abc, container, false);
mContext = rootView.getContext();
new Thread(new Runnable() {
#Override
public void run() {
try {
((Activity) mContext).runOnUiThread(new Runnable() {
#Override
public void run() {
gotstatdata = false;
Calculations(128);
gotstatdata = true;
}
});
} catch (Exception e) {
}
}
}).start();
You are dismissing your dialog as soon as it runs with ringProgressDialog.dismiss();.
That line should be removed and you should do something like send out a broadcast when you are finished to close the progress dialog.
It looks like you might also just want a background thread rather than a second Activity because no user interaction is required.
Looking at AsyncTask would be the easiest way for you to start with background threads and it's 'onPostExecute` method will allow you to dismiss your dialog.
Edit
The basic structure you want is to add the following
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
//TODO: show your dialog from here
}
#Override
protected Void doInBackground(Void... params) {
//TODO: call Calculations(128); from here
//Calculations(128); should live within this async task instead of a new activity
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//TODO: call dismiss on your dialog from here
}
}.execute();
instead of the new Thread(new Runnable() { block you currently have

Unable to close Progress Dialog in Android

I want to create a progress dialog in android, keep it open for 2 seconds then close it.
Below is the code I have written:
package com.example.proressdialogtest;
import android.app.activity;
import android.app.ProgressDialog;
import android.os.bundle;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ProgressDialog pg = new ProgressDialog(MainActivity.this, 4);
pg.show(MainActivity.this, null, "Searching...", false);
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
pg.dismiss();
}
When I run the code on my device, the ProgressDialog is opened and then it stays open, it does not close after 2 seconds. What am I doing wrong?
As per the answers below. I have added the onPreExecute() and onPostExecute() methods before and after the doInBackground method respectively.
Here is the code for the two methods.
ProgressDialog pd;
public void onPreExceute() {
pd = new ProgressDialog(MainActivity.this);
pd.show(MainActivity.this, "", "Searching...", false);
}
public void onPostExecute() {
pd.dismiss();
}
The problem still persists. The progress bar will not close.
You are calling sleep() on the UI Thread. Don't do this. Use runOnUiThread or an AsyncTask
Painless Threading
AsyncTask
I would use an AsyncTask. You will start the ProgressDialog in onPreExecute() then close it in onPostExecute(). It is a UI element so using it in doInBackground() will give you the error
This SO answer has a good example of using it
Edit:
Use this code
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final ProgressDialog pg ;
pg = ProgressDialog.show(MainActivity.this, null, "Searching...", false);
Thread timer=new Thread(){
public void run()
{
try {
sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
finally{
pg.dismiss();
}
}
};
timer.start();
}
}
You are pausing the UI thread by calling
Thread.sleep(2000);
Because Thread.sleep(x) will make the current thread sleep for x milli seconds
which is bad thing.
place
I don't know why
pg.dismiss() in finally block to make sure that progress dialog will be closed.
And also your Code won't run because you have missed
super.onCreate(savedInstanceState);
following code will work for you,
new AsyncTask<Integer, Integer, Boolean>()
{
ProgressDialog progressDialog = null;
#Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(MainActivity.this, "",
"Loading...");
}
#Override
protected Boolean doInBackground(Integer... params)
{
if (params == null)
{
return false;
}
try
{
Thread.sleep(2000);
}
catch (Exception e)
{
return false;
}
return true;
}
#Override
protected void onPostExecute(Boolean result)
{
progressDialog.dismiss();
}
}.execute();

Android - Create Progress Dialog

I am new to android development. I want to develop a dialog with a progressbar in my application. When i click the search button the dialog should appear with the progressbar, showing that the progress is going on before switching to another activity. Please suggest me with sample code.
Use a ProgressDialog. You should do the work on a new thread, though, and use a handler to call back to the activity when finished. Here's how I do it:
private ProgressDialog pd;
private View.OnClickListener searchClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
pd = ProgressDialog.show(MyActivity.this, "Searching...", "Searching for matches", true, false);
new Thread(new Runnable() {
public void run() {
//do work
//.....
finishedHandler.sendEmptyMessage();
}
}).start();
}
}
private Handler finishedHandler = new Handler() {
#Override public void handleMessage(Message msg) {
pd.dismiss();
//start new activity
}
}

ProgressDialog Problem?

There is a ProgressDialog in my app. It is running but after finishing process does not close. Where is the error, I'm doing.
Thanks.
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
progressdialog.show();
new Thread(new Runnable() {
public void run() {
try {
// doing something...
progressdialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
do this......
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
progressdialog.show();
new Thread(new Runnable() {
public void run() {
try {
// doing something...
hm.sendEmptyMessage(0);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
Handler hm = new Handler()
{
public void handleMessage(Message msg)
{
progressdialog.dismiss();
}
}
Thanks.
progressdialog.setVisible(false);
if pricessdialog instanse of JDialog
Call progressdialog.dismiss(); from the main thread;
The right way of doing any work in background while showing the progress dialog is using AsyncTask with ProgressDialog bounded. See here. Remember, that you can not modify the UI from the thread, which is not UI thread.
The process dialog can also be dismissed by calling following method.
progressdialog.cancel();

Categories

Resources