When you need to do some tasks that need loading like network operations or something that might look like your application is stuck, you should use a separate thread for this. But this other thread cannot update the graphical interface, IE, it should not change the content of any visual component. To get around this problem, there is the Asynctask class, which allows us to implement the code that will be executed before, during and after performing some task performed in the background, you can place this class within your Activity. Follows code
import android.location.Location;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
public class testeActivity extends ActionBarActivity {
ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teste);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
}
private void exibirProgress(boolean exibir) {
mProgressBar.setVisibility(exibir ? View.VISIBLE : View.GONE);
}
class MinhaTask extends AsyncTask<Location,Void,Location> {
@Override
protected void onPreExecute() {
super.onPreExecute();
exibirProgress(true);
}
@Override
protected Location doInBackground(Location... params) {
return medotodoRetornaLocation();
}
@Override
protected void onPostExecute(Location location) {
super.onPostExecute(location);
exibirProgress(false);
metodoAtualizaAInterfaceGrafica(location);
}
}
}
The display method displayProgress(Boolean); is the method responsible for whether or not to display the Progress in the view.
Preview statement in view:
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
android:layout_centerInParent="true"
android:visibility="gone"
android:id="@+id/progressBar"
/>
Now add the methods that update the graphical interface and what will fetch the location and be happy! o/
To learn more about the Asynctask class follow link:
http://developer.android.com/reference/android/os/AsyncTask.html
Here is an example, which language you are using, and java? https://docs.oracle.com/javase/tutorial/uiswing/components/progress.html
– Ivan Ferrer
Yes, and for android
– Joab