Loading until datas are available - android

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.

Related

Fix ProgressDialog showing up after download is complete

I have a ListView that is populated with articles. When the user long clicks on an article, I want to download its summary and display it to the user. I have a function in AsyncTask (NOT doInBackground...) that takes care of the download. During the downloading process, I want to display a ProgressDialog. Since the download is on the main thread, there is a delay when I long click an article and the ProgressDialog shows AFTER the download is complete. How can I get it to show DURING the download?
I have tried these methods but I don't really understand it since I am a beginner in Android Development. The app freezes while its downloading and shows the Dialog after it's done.
ProgressDialog shows up after thread is done
display progressdialog while listview is loading
HomeActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
trendingList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
try {
...
} catch (Exception e) {
String link = homeLinks.get(position);
//TASK TO DOWNLOAD SUMMARIES FROM ASYNCTASK
task.downloadSummary(link);
adapter.notifyDataSetChanged();
}
return true;
}
});
Background Class
public class BackgroundTask extends AsyncTask<String, Void, String> {
ProgressDialog progress = new ProgressDialog(HomeActivity.this);
//SHOW DIALOG AND THEN DOWNLOAD SUMMARIES
public void downloadSummary(String address){
progress.setMessage("Downloaded summary ");
progress.show();
...
When initializing an AsyncTask you must allocate the 3 dependent methods of this thread, onPreExecute that will allocate your ProgressDialog, the InBackground that put the job you want to perform and the onPostExecute that reotrna the result of your work and you close your progressDialog
It had stayed like this
public class BackgroundTask extends AsyncTask<String, Void, String> {
ProgressDialog progress = new ProgressDialog(HomeActivity.this);
#Override
protected void onPreExecute() {
super.onPreExecute();
progress.setMessage("Downloaded summary ");
progress.show();
}
#Override
protected void doInBackground(String... strings) {
//Ação do Trabalho
}
#Override
protected void onPostExecute(String result) {
progress.dismiss();
Log.e("Work", "Result "+result);
}
}
it is mandatory the google background method does not like when ignoring this use being an AsysnTask will have to use it. If on the contrary if it is a fast job use a thread invoked with run

how to load asyctask if new image is arrived in device ?

i have designed custom gallery but problem is that every time when i open gallery asyctask is call and all image getting load again. i want to load asyctask if new images arrived into device otherwise directly gallery will be shown without load
i have no more idea that how to do this kind of programming stuff so if there is anyway to do this kind of stuff then please suggest me
is there any way to do this ... ??
private class AsyncTaskForListview extends
AsyncTask<String, String, String> {
private String responce;
ProgressDialog progressDialog;
#Override
protected String doInBackground(String... params) {
getAllGalleryFolderName(); // process for get image
return responce;
}
#Override
protected void onPostExecute(String result) {
lvShowAllImageFolder.setAdapter(effectImageAdapter); // set adapeter in listview
effectImageAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog
.show(DisplayGalleryActivity.this,
getResources().getString(
R.string.progress_dialog_heading),
getResources().getString(
R.string.progress_waiting_message));
}
#Override
protected void onProgressUpdate(String... text) {
}
}
You can use the FileObserver class to monitor a file or fold. It will notify if a change by any process in the device occurs. More details here .

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.

Stop Loading Screen in android as soon as data loaded from mysql

I am working on android project in which I have to apply loading screen until the data loads from database. I know that how to start proegressBar but I need to know how can I stop the loading screen as data loaded from database.If u can please give me one simple example from which I can understand that how can I do this because I am new to android.
Since you are getting data from a database you should do it asynchronously.
The best way to do it would be using an AsyncTask.
class GetDataTask extends AsyncTask<Void, Void, Void>{
ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("Loading...");
dialog.setIndeterminate(true);
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
//get data from DB here
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
And to make it execute: new GetDataTask().execute();

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

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

Categories

Resources