user1692261
user1692261

Reputation: 1217

android - connect to server screen

I have an app that on startup it communicates with my server and gets some data.

I want to add some "wait window" to the homapage of the app that indicate that such communication is taking place. I want that window to pop up and the rest of the home page will be faded in the background.

In case of success fetching the data this window will disappear and from there it will be normal use. If there was an error, or a connection with the server can not be established I want to give 2 options: Retry or close app.

My main challenge is the that window popping and make the homepage to be faded in the background. Any ideas how to do it?

Upvotes: 0

Views: 78

Answers (1)

Ariel Magbanua
Ariel Magbanua

Reputation: 3123

Fire up a progress dialog during the execution of AsyncTask where it fetches data from your server and dismiss the dialog after fetching all the data.

Your code might be something like this.

class LoadFeed extends AsyncTask<Void,Void,Void>{

    private Dialog progressDialog;
    Context context;

    public LoadFeed(Context context){
        progressDialog = getProgressDialog(context, "Updating Items", "Fetching updates...");
        this.context = context;
    }

    public void onPreExecute() {
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        //fetching of data

        return null;

    }

    public void onPostExecute(Void unused) {
        progressDialog.dismiss();
    }

}

public Dialog getProgressDialog(Context context, String title, String msg){

     ProgressDialog dialog = new ProgressDialog(context);
     dialog.setTitle(title);
     dialog.setMessage(msg);
     dialog.setIndeterminate(true);
     dialog.setCancelable(true);

     return dialog;
}

Upvotes: 1

Related Questions