How to put animation "wait" on Android?

Asked

Viewed 880 times

2

I wonder how I put an animation of "Wait" while doing a heavy task on Android. In case, when the user type the zip code I want to run an animation asking to wait while I make the request.

private Activity context;
private ProgressDialog progress;

private class BuscaCepTask extends AsyncTask<String, Void, String> {

    protected void onPreExecute() {
        progress = ProgressDialog.show(context, "Aguarde...", "Buscando CEP...", true, true);
    }

    URL url = null;
    HttpURLConnection httpURLConnection = null;

    @Override
    protected String doInBackground(String... params) {
        StringBuilder result = null;
        int respCode = -1;

        try {
            url = new URL("http://cep.correiocontrol.com.br/" + params[0] + ".json");
            httpURLConnection = (HttpURLConnection) url.openConnection();

            do {
                if (httpURLConnection != null) {
                    respCode = httpURLConnection.getResponseCode();
                }
            } while (respCode == -1);

            if (respCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                result = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    result.append(line);
                }
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
                httpURLConnection = null;
            }
        }

        return (result != null) ? result.toString() : null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        try {
            JSONObject object = new JSONObject(s);
            EditText rua = (EditText) findViewById(R.id.rua);
            rua.setText(object.getString("logradouro"));

        } catch (JSONException e) {
            e.printStackTrace();
        }
        progress.dismiss();
    }
}

.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.formulario);

    final EditText endereco = (EditText) findViewById(R.id.endereco);
    endereco.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            BuscaCepTask buscarCep = new BuscaCepTask();
            buscarCep.execute(endereco.getText().toString());
        }
    });
  • You want to know how to do the animation or want to know where to start the animation?

  • both of them! I’ll have to create a class?

  • 1

    Animation should be started in the method onPreExecute() and stop at the onPostExecute(). There are many ways to make animations on Android I suggest you read first about the subject matter and if you then have any more concrete questions ask a question.

  • I can call the onPreExecute() method inside onCreate?

  • 1

    You don’t "call" the onPreExecute, he is a method of his own AsyncTask, you only need to overwrite within your class and within your cycle it will run first. In your own class you can use the ProgressDialog to obtain an interface indicating the "carrying...".

  • I edited my post. This is more or less how it should look?

  • 1

    Theoretically yes, to be sure, it is good to put the note @Overrride in the method onPreExecute. Have you tested? This is how you will know if it will work.

  • It’s not working, when I start typing the zip code in the form the app hangs and stops working. Someone has an idea?

  • 1

    Each time a number is entered you are calling the webservice. You need to restrict this to be done only when all 8 digits are informed.

  • if (address.toString(). length() == 8) ?

  • Thank you to everyone who helped me. It worked now.

Show 6 more comments

2 answers

3


There’s the solution in code:

private class uploadPhoto extends AsyncTask<Void, Void, Void>{

            private ProgressDialog dialog;
        protected void onPostExecute(Void dResult) {

            dialog.cancel();

    }

    protected void onPreExecute() {


        dialog = new ProgressDialog(Myactivity.this);
        dialog.setCancelable(true);
        dialog.setMessage("uploading...");
        dialog.show();

            }

    protected Void doInBackground(Void... params) {
        // call upload photo here.
    }

}

To call the asyncTask use:

new uploadPhoto().execute();
  • But this way the dialog starts as soon as the class is called. I want the animation to appear only when I fetch the zip code.

  • 1

    Then just call new researchCep(). execute(); and implement this animation within the ZIP research class ai yes it starts only when doing research.

  • Help me implement this into my code.

  • Sure, man, I’ll help you.

  • Play Github I help you with the code, you’ll see it’s very simple, hugs.

  • Thanks for the good will, but I’ve got it. Hug!

  • Whoa, vlw dude anything we’re there.

  • 1

    I edited the answer. If you want to debate what bothered you, the place is the [goal].

Show 3 more comments

1

The answer was chosen from above, but how it worked I will show how was my code corrected.

private String caminhoArquivo;
private ProgressDialog dialog;

private class BuscaCepTask extends AsyncTask<String, Void, String> {

    protected void onPreExecute() {
        dialog = new ProgressDialog(Formulario.this);
        dialog.setCancelable(true);
        dialog.setMessage("Buscando CEP...");
        dialog.show();
    }

    URL url = null;
    HttpURLConnection httpURLConnection = null;

    @Override
    protected String doInBackground(String... params) {
        StringBuilder result = null;
        int respCode = -1;

        try {
            url = new URL("http://cep.correiocontrol.com.br/" + params[0] + ".json");
            httpURLConnection = (HttpURLConnection) url.openConnection();

            do {
                if (httpURLConnection != null) {
                    respCode = httpURLConnection.getResponseCode();
                }
            } while (respCode == -1);

            if (respCode == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                result = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    result.append(line);
                }
                br.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
                httpURLConnection = null;
            }
        }

        return (result != null) ? result.toString() : null;
    }

    @Override
    protected void onPostExecute(String s) {
        dialog.cancel();
        super.onPostExecute(s);
        try {
            JSONObject object = new JSONObject(s);
            EditText rua = (EditText) findViewById(R.id.rua);
            rua.setText(object.getString("logradouro"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.formulario);

    final EditText endereco = (EditText) findViewById(R.id.endereco);
    endereco.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (endereco.length() == 8) {
                BuscaCepTask buscarCep = new BuscaCepTask();
                buscarCep.execute(endereco.getText().toString());
            }
        }
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.