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?
– ramaral
both of them! I’ll have to create a class?
– Felipe Miranda
Animation should be started in the method
onPreExecute()
and stop at theonPostExecute()
. 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.– ramaral
I can call the onPreExecute() method inside onCreate?
– Felipe Miranda
You don’t "call" the
onPreExecute
, he is a method of his ownAsyncTask
, you only need to overwrite within your class and within your cycle it will run first. In your own class you can use theProgressDialog
to obtain an interface indicating the "carrying...".– Paulo Rodrigues
I edited my post. This is more or less how it should look?
– Felipe Miranda
Theoretically yes, to be sure, it is good to put the note
@Overrride
in the methodonPreExecute
. Have you tested? This is how you will know if it will work.– Paulo Rodrigues
It’s not working, when I start typing the zip code in the form the app hangs and stops working. Someone has an idea?
– Felipe Miranda
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.
– Paulo Rodrigues
if (address.toString(). length() == 8) ?
– Felipe Miranda
Thank you to everyone who helped me. It worked now.
– Felipe Miranda