Display "Loading : Progress Bar " in Android Intent till Data Load - android

I am working on an android app, in that app i have intent2 which on click redirects to intent3 and takes some time then loads a table and displays server data into it.
Sometimes if there is a lot of data, it tales pretty much time to get the dataload and the time blank screen is displayed increases.
i wish to show a loading bar till the data loads.
how can i show the ProgrssBar till only when data is not displayed ?

Probably your best bet would be to use AsyncTask in your "intent3":
You could do it like this:
private class performBackgroundTask extends AsyncTask <Void, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(ClassName.this);
protected void onPreExecute()
{
Dialog.setMessage("Please wait...");
Dialog.show();
}
protected void onPostExecute(Void unused)
{
try
{
if(Dialog.isShowing())
{
Dialog.dismiss();
}
// do your Display and data setting operation here
}
catch(Exception e)
{
}
#Override
protected Void doInBackground(Void... params)
{
// Do your background data fetching here
return null;
}
}

You probably need to run an AsyncTask on onCreate when you open the new activity, the structure of the asynctask would be like this (taken from the google doc), notice that if you want to increament a progress bar you have to implement onProgressUpdate and call publishProgress in the doInBackground method
private class DownloadFilesTask extends AsyncTask<Void, Integer, Void> {
protected void onPreExecute()
{
// show your progress bar
}
protected Void doInBackground(Void... params) {
// do your work and publish the progress
publishProgress(progress);
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Void result) {
//dismiss your progress bar
}
}
This code is just an example, of course you need to adapt it to your logic/code.
Check out this simple and complete example

Related

Fetch data in background and display in UI

I'm using volley to fetch data using volley and display in User Interface, and it takes time to load the data. What i want to happen is, I want to fetch the data in background during the splash screen or loading screen and display it in User interface. I want the fetching part to be done in another activity and it should be a background service and this should be called in "main activity" to populate the fields.
You can try using the AsyncTask. So the process goes like that:
In onPreExecute() you can show a ProgresDialog or a ProgresBar to visualize your loading process
in doInBackground(Params...) you start loading your data
and in onPostExecute(Result) you display your data in UI and hide your ProgresDialog/ProgresBar.
Example of AsyncTask usage.
Do it like this:
private class LoadDataBaseData extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//Start loading your data
return "Data loaded";
}
#Override
protected void onPostExecute(String result) {
//Update your UI with data you loaded/start your activity with loaded data
}
#Override
protected void onPreExecute(){
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
use AsyncTask like that
private class LongOperation extends AsyncTask<String, Void, String>
{
#Override
protected String doInBackground(String... params)
{
//do your work here
return "Executed";
}
#Override
protected void onPostExecute(String result)
{
progressBar.dismiss();
}
#Override
protected void onPreExecute()
{
progressBar = ProgressDialog.show(getActivity(), null, "message...");
progressBar.show();
}
#Override
protected void onProgressUpdate(Void... values)
{
}
}

show progress bar while loading images in android

how to show progress bar while loading images. I have tried the code given below
Picasso.with(getActivity())
.load(stage1ImageURL)
.placeholder(android.R.layout.simple_spinner_item)
.error(R.drawable.no_image).into(stage1ImageView);
Take a look at the official docs how to deal with a progress bar.
In your case, you have to write an AsyncTask, which starts the dialog in onPreExecute() and ends in onPostExecute().
Try this, it uses AsyncTask:
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
#Override
protected void onPreExecute()
{
//show loading dialog
progressDialog= ProgressDialog.show(YourActivity.this, "Progress Dialog Title Text","Process Description Text", true);
//do initialization of required objects objects here
};
#Override
protected Void doInBackground(Void... params)
{
//do loading operation here
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}
You can call this using from your onCreate():
LoadData task = new LoadData();
task.execute();
You have to use
progressBar.setVisibility(View.VISIBLE);
to show progressBar. And to hide it,
progressBar.setVisibility(View.GONE);
Follow this link for complete code.

Loading until datas are available

I'm making an application, which only pick some datas on a website. But the problem is that the layout does not show up until the datas are available, the application seem to be blocked while it is looking for the datas. I tried to put the content view in the oncreate and then change the text in the onstart, when I have the datas, but the application still blocks.
Is this possible to print a default text, and then change it when the application have the datas?
Try this, it uses AsyncTask:
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
#Override
protected void onPreExecute()
{
//show loading dialog
progressDialog= ProgressDialog.show(YourActivity.this, "Progress Dialog Title Text","Process Description Text", true);
//do initialization of required objects objects here
};
#Override
protected Void doInBackground(Void... params)
{
//do loading operation here
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}
You can call this using from your onCreate():
LoadData task = new LoadData();
task.execute();
Have you tried AsyncTask to download data from website (API)? It will show you progressdialog until data is not downloaded.

Android Progress Bar appear and hide

I have a progress bar and I do not want to incorporate a numerical value to show the loading of some content. I just want that the ProgressBar should appear, animate and then go away.
I used:
public void buttonClick(View v){
ProgressBar mProgress=(ProgressBar)findViewById(R.id.my_progress);
mProgress.setVisibility(VISIBLE); //line 1..
//loading data from web... takes time
mProgress.setVisibility(INVISIBLE); //line 2..
}
but when I run this code, both line 1 and line 2 executes, but the UI changes afterwards, which is not desired. I want that when the button is clicked, the progress bar should appear and when the data is downloaded from the web the progress bar should disappear.
I tried setting the visibility from another thread, but it didn't work as UI changes are not allowed in other threads.
You download your data in AsyncTask right? Put this code
private class DownloadData extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgress.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... params) {
// download your data here
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mProgress.setVisibility(View.GONE);
}
}
The best way implement it by using an AsyncTask
MyTask.java
class MyTask extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
Context context;
MyTask(Context context){
this.context=context;
}
#Override
protected void onPreExecute() {
dialog=new ProgressDialog(context);
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
//do your task here
}
#Override
protected void onPostExecute(Void result) {
if(dialog.isShowing()){
dialog.dismiss();
}
}
}
You can start the task like this:
public void buttonClick(View v){
new MyTask(YourActivity.this).execute();
}
You can modify the AsyncTask accordingly to retrieve the result.
Hope it helps. :)

Android AsyncTask onPreExecute not called indeterminantly

I have an AsyncTask that is supposed to show a progress bar while it uploads some stuff via Internet. Sometimes it works like a charm and sometimes it does not show any progress bar. Here is the code:
public class Upload extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(Activity.this);
protected void onPreExecute() {
dialog = ProgressDialog.show(Activity.this, "wait...", "", true, true);
}
#Override
protected Void doInBackground(Void... params) {
//upload stuff
return null;
}
protected void onPostExecute(Void result) {
try {
if (dialog.isShowing())
dialog.dismiss();
dialog = null;
} catch (Exception e) {
// nothing
}
Intent next = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(next);
}
}
}
The doInBackground and onPostExecute work always, and sometimes altogether it works like a charm. But sometimes, there is no progress bar while it is uploading. Is this a race condition? I do not think so, but I cannot find any explanation.
You're creating the object twice in the class. The ProgressDialog.show already returns a created ProgressDialog object, but you have instantiated it first at the top. The ProgressDialog should be instantiated once, so try removing the instantiation at the top and try again, like so:
private ProgressDialog dialog;
protected void onPreExecute() {
dialog = ProgressDialog.show(Activity.this, "wait...", "", true, true);
}
Maybe it is because void parameter that causes that problem. Just try to use Integer as your parameters.:
public class Upload extends AsyncTask<Integer, Integer, Integer>

Categories

Resources