Answering your question:
Correct is to use an extended class of AsyncTask
to display a progress bar or an image, or whatever you want, and render in background
.
In addition, because of the runtime, as raised by @ramaral (a user who knows Android too), I created the topic:
When using AsyncTask, ThreadPoolExecutor, or Service
Why use it?
Basically because Android after a few seconds of processing, it can recognize that your APP has stopped, so if you create an asynchronous class that runs background
.
A quick and brief explanation of the AsyncTask class:
Class Properties:
AsyncTask<Parâmetros,Progresso,Resultado>
Main class methods:
onPreExecute
: will execute before starting the task ( doInBackground
);
doInBackground
: will execute your task;
onProgressUpdate
: will execute during the execution of your task;
onPostExecute
: will execute after the task finishes.
Example of a% horizontal% with AsyncTask class:
Class progressBar
:
public class CalculoMatematico extends AsyncTask<Void, Integer, String> {
Context ctx;
ProgressBar progressBar;
public CalculoMatematico(Context ctx, ProgressBar progressBar) {
this.ctx = ctx;
this.progressBar = progressBar;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
this.progressBar.setProgress(0);
this.progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
this.progressBar.setVisibility(View.GONE);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
this.progressBar.setProgress(values[0]);
}
@Override
protected String doInBackground(Void... params) {
// Seu código aqui
// Aqui você seta o valor máximo
this.progressBar.setMax(valor);
// Aqui publica o valor atual, conforme atualiza
publishProgress(valor);
}
Class CalculoMatematico
:
public class Main extends AppCompatActivity {
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.progressBar = findViewById(R.id.progressBarId);
// Aqui você esconder sua progressBar ao iniciar ou fazer isso direto no xml
this.progressBar.setVisibility(View.GONE);
}
public void calcular() {
CalculoMatematico calculo = new CalculoMatematico(this, this.progressBar);
calculo.execute();
}
XML Main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.exemplo.recebejsonsqlite.Main">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:progress="0"
android:progressTint="@android:color/holo_blue_dark" />
...Seus outros elementros...
</LinearLayout>
References to reading:
AsyncTask Official Documentation
Example of using AsyncTask