tjm1706 tjm1706 - 1 year ago 216
Android Question

Android osmdroid / osmbonuspack - show progress spinner while loading a map

When using Android osmdroid/osmbonuspack:

How can I show a progress dialog (or a spinner) while loading the map?
It should finish when I put all items on the map as well.

Answer Source

This answer is ONLY !!! useful when building the UI takes quite some time. Then you may show a temporary popup indicating that work is in progress.

My first guess was of course adding a ProgressBar in the Layout file using a RelativeLayout. Building the OSM map with many attributes prohibited showing the ProgressBar.

Normally a ProgressBar or anything similar only works correctly if the heavy work is done in the background. Alas, not possible because of a third party package.

In my case I use the OsmBonusPack (OSM map) and I have to plot many items on the map.

Now showing that the building of the map is in progress you can do the following (universal) trick:

Step 1: make a MessageDialog (showing a message) that extends DialogFragment:

public class MessageDialog extends DialogFragment {
    ProgressDialog mDialog = null;
    public MessageDialog () {
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        mDialog = new ProgressDialog( getActivity());
        this.setStyle( STYLE_NO_TITLE, getTheme()); 
        mDialog.setMessage("Building the map ... "); 
        return mDialog;

Step 2: When you start the heavy UI work (e.g. building the map (with many items on it) start showing the MessageDialog and then wait a short time like 300msecs or so IN THE BACKGROUND. This will allow the message box to appear!

FragmentManager fm = getActivity().getSupportFragmentManager();
myInstance = new MessageDialog (); fm, "some_tag");
new AsyncTask<Void, Void, Void>() {
    protected Void doInBackground( Void... voids ) {
        try { Thread.sleep( 300); } catch( Exception e) {}
        return null;
    protected void onPostExecute( Void msg) {
        // OPTION A: 
        start navigating to the map screen ... and do there the  myInstance.dismiss();
        // OPTION B: 
        do the work on the UI here AND myInstance.dismiss();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download