I have the code bellow when I click button it display a progress bar dialog.
public class MyAndroidAppActivity extends Activity {
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
btnStartProgress.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
}
});
}
// file download simulator... a really simple
public int doSomeTasks() {
Intent intent = new Intent(MyAndroidAppActivity.this, Accueil.class);
startActivity(intent);
}
}
But I want to display it(progress bar dialog) in start of my android application.
How can I do this ? Thanks in advance for any help.
Related
I've used a splash screen and progress bar in my Android App.But after splash disappears,then next screen becomes black before switching to my main activity. But I don't want black screen.Can anyone please explain what's going on here and how can I prevent that black screen ? This is my Splash Java class.
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
startApp();
finish();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
I think you could remove the finish() inside the Thread.
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
startApp();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
Approach 1:
If you required splash for few seconds:
new Thread((new Runnable() {
#Override
public void run() {
try { Thread.sleep(5500); }catch(Exception e) {}
startApp();
finish();
}
}
)).start();
Approach 2: Show next screen when splash finish:
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
startApp();
finish();
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
Try this one:
public class Splash extends AppCompatActivity {
private ProgressBar mProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mProgress = (ProgressBar) findViewById(R.id.splash_screen_progress_bar);
new Thread((new Runnable() {
#Override
public void run() {
doWork();
}
}
)).start();
}
private void doWork() {
for (int progress = 0; progress < 100; progress += 10) {
try {
Thread.sleep(5500);
mProgress.setProgress(progress);
} catch (Exception e) {
e.printStackTrace();
}
}
startApp();
}
private void startApp() {
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
finish();
}
}
I have the MainActivity as this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bo = new Operation(getApplicationContext());
}
public void op_perform(View v) throws Exception { //call this when a button is pressed
try {
bo.demo();
} catch (Exception e) {
// TODO: handle exception
}
The Operation class has the following lines of code:
Context con;
Operation(Context ac) {
con = ac;
barProgressDialog = new ProgressDialog(con);
updateBarHandler = new Handler();
}
public void demo() {
barProgressDialog = new ProgressDialog(con);
barProgressDialog.setTitle("Downloading Image ...");
barProgressDialog.setMessage("Download in progress ...");
barProgressDialog.setProgressStyle(barProgressDialog.STYLE_HORIZONTAL);
barProgressDialog.setProgress(0);
barProgressDialog.setMax(20);
barProgressDialog.show();
...
But, i'm not getting the progress dialog at all in my screen. I want the progress dialog to be a determinate horizontal one. This is just a piece of code and i need to put these in the Asyn task. But the processdialog is not even showing. Recommended solutions. I'm a beginner to android.
Here is a complete example of what you need: http://turboprogramacion.blogspot.cl
private ProgressDialog barraProgreso;
private Handler handler;
private void mostrarBarraProgreso(){
barraProgreso = new ProgressDialog(MainActivity.this);
barraProgreso.setTitle("Buscando...");
barraProgreso.setMessage("Progreso...");
barraProgreso.setProgressStyle(barraProgreso.STYLE_HORIZONTAL);
barraProgreso.setProgress(0);
barraProgreso.setMax(10);
barraProgreso.show();
handler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
try {
while (barraProgreso.getProgress() <= barraProgreso.getMax()) {
Thread.sleep(1000);
handler.post(new Runnable() {
#Override
public void run() {
barraProgreso.incrementProgressBy(1);
}
});
if(barraProgreso.getProgress() == barraProgreso.getMax()){
barraProgreso.dismiss();
}
}
}catch (InterruptedException er){
er.printStackTrace();
}
}
}).start();
}
I have two class. class A is activity where my progress bar will be use. and class B is no-activity where my progress bar will be update. but when i calling progress bar from non-activity class B . i got null pointer exception.
class A:-
ProgressBar progressBar;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);}
class B:-
new Thread(new Runnable() {
int i = 0;
int progressStatus = 0;
public void run() {
while (progressStatus < 100) {
progressStatus += doWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
((Activity) cnt).runOnUiThread(new Runnable() {
public void run() {
XMPPClient xc = new XMPPClient();
xc.progressBar = new ProgressBar(cnt);
xc.progressBar.setProgress(progressStatus);
// Toast.makeText(cnt, "ok", Toast.LENGTH_SHORT).show();
i++;
}
});
}
}
private int doWork() {
return i * 3;
}
}).start();
when i added this line :- xc.progressBar = new ProgressBar(cnt);
then i did not get nullpointerexception. But now my progress bar is **not updating.**
please any one help me.
ProgressBar progressBar;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
ClassB classb = new ClassB(this, progressBar);
}
public class ClassB{
private Context cnt;
private ProgressBar progressBar;
public ClassB(Context context, ProgressBar pBar){
cnt = context;
progressBar = pBar;
}
}
Now you can use progressBar instead of creating a new one like you did in previous code!
Just a quick overview...
in ClassA :-
ProgressBar progressBar;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
////Then Use the Reference on Progressbar
ClassB classb = new ClassB(this, progressBar);
}
Then in ClassB :-
public class ClassB{
private Context cnt;
private ProgressBar progressBar;
public ClassB(Context context, ProgressBar pBar){
cnt = context;
progressBar = pBar;
new Thread(new Runnable() {
int i = 0;
int progressStatus = 0;
public void run() {
while (progressStatus < 100) {
progressStatus += doWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
((Activity) cnt).runOnUiThread(new Runnable() {
public void run() {
XMPPClient xc = new XMPPClient();
xc.progressBar = new ProgressBar(cnt);
xc.progressBar.setProgress(progressStatus);
// Toast.makeText(cnt, "ok", Toast.LENGTH_SHORT).show();
i++;
}
});
}
}
private int doWork() {
return i * 3;
}
}).start();
}
How to bring a progress bar on clicking a button in an activity i have an button in my main activity and if i click the button i want to display a progress bar i dont wanna use a secondary activity for this i have done with 2 activities but i need it to be done in main activity itself.
thank you
My code:
Home Activity :
public void load(View v){
Intent intent = new Intent(this,Splash.class);
this.startActivity(intent);
}
Splash Activity :
public class Splash extends Activity {
private long ms = 0;
private long splashTime = 20000;
private boolean splashActive = true;
private boolean paused = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread mythread = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if (!paused)
ms = ms + 100;
sleep(100);
}
} catch (Exception e) {
} finally {
Intent intent = new Intent(Splash.this, Home.class);
startActivity(intent);
}
}
};
mythread.start();
}
}
You can do like this
ProgressDialog progress;
progress = new ProgressDialog(this);
progress.setTitle("Please Wait!!");
progress.setMessage("Wait!!");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
call this code where you want to display your ProgressBar.
There is one more method to show the ProgressBar on Button Click.
Put this code in your xml
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />
and just write following code on your Button click in java file.
progressBar.setVisibility(View.VISIBLE);
It will show the Progressbar on your Button Click.
I believe the user2306156 will not be in need of this now, hopefully someone who is looking for this will be glad to know i guess.
put this code in your xml
<ProgressBar
style="#android:style/Widget.Material.Light.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkBox"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:id="#+id/progressBar" />
put this in your activity code
spinner=(ProgressBar)findViewById(R.id.progressBar);
spinner.setVisibility(View.GONE);
and this in your onClick of the activity code
spinner.setVisibility(View.VISIBLE);
See this Android Progress Bar Example
public class MyAndroidAppActivity extends Activity {
Button btnStartProgress;
ProgressDialog progressBar;
private int progressBarStatus = 0;
private Handler progressBarHandler = new Handler();
private long fileSize = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
btnStartProgress = (Button) findViewById(R.id.btnStartProgress);
btnStartProgress.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
// prepare for a progress bar dialog
progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("In Progress ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
//reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// ok, file is downloaded,
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
}
});
}
// file download simulator... a really simple
public int doSomeTasks() {
while (fileSize <= 1000000) {
fileSize++;
if (fileSize == 100000) {
return 10;
} else if (fileSize == 200000) {
return 20;
} else if (fileSize == 300000) {
return 30;
}
// ...add your own
}
return 100;
}
}
Another Option is to use the nifty Spezi-Views, it contains a ProgressButton which is quite easy to use:
<de.halfreal.spezi.views.ProgressButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press me"
app:selectedText="I am loaded"
app:unselectedText="Press me again"
app:loadingDrawable="#drawable/spinner"
/>
and in code:
...
//show a rotation spinner, and no text (or the loading text)
progressButton.enableLoadingState();
//show no animation, but the selected/ unselected text
progressButton.disableLoadingState();
...
I use this Tutorial to create custom Progressbar and it works .
But I want to start new activity when progress bar go to 100% .
anyone can help to put start new activity code in correct place?
You can check if the progress has reached the maximum possible while you're setting it, like this:
#Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
// the setProgress super will not change the details of the progress bar
// anymore so we need to force an update to redraw the progress bar
invalidate();
if(progress >= getMax()) {
Intent intent....
}
}
You can call the onContinue function timer thread and set the intent to next activity and register the activity name in manifest file.
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
mProgressBar = (ProgressBar)findViewById(R.id.adprogress_progressBar);
final Thread timerThread = new Thread() {
#Override
public void run() {
mbActive = true;
try {
int waited = 0;
while(mbActive && (waited < TIMER_RUNTIME)) {
sleep(200);
if(mbActive) {
waited += 200;
updateProgress(waited);
}
}
} catch(InterruptedException e) {
} finally {
onContinue();
}
}
};
timerThread.start();
}
#Override
public void onDestroy() {
super.onDestroy();
}
public void updateProgress(final int timePassed) {
if(null != mProgressBar) {
final int progress = mProgressBar.getMax() * timePassed / TIMER_RUNTIME;
mProgressBar.setProgress(progress);
}
}
public void onContinue() {
Intent intd=new Intent(this,MainActivity.class);
startActivity(intd);
}
Try this...
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 5;
// Update the progress bar and display the current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText("Loading "+progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds. Just to display the progress slowly
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
**Intent intent = new Intent(Main_Activity.this, Send_Email.class);
startActivity(intent);**
}
}).start();