1

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.

tm1701
  • 7,307
  • 17
  • 79
  • 168
  • Do you have a listener or something to detect when the map is initialized ? – David Seroussi Apr 22 '16 at 17:57
  • I think the problem is that I build/show the map on the UI thread. Then nothing gets done. Correct? – tm1701 Apr 24 '16 at 10:55
  • I don't think so. I personnally don't use osmdroid but I think it has nothing to do with the thread. Look at my answer, you can build you own custom listener and be able to catch the moment your map is initialized. – David Seroussi Apr 24 '16 at 11:04

2 Answers2

1

You can create your own event like this.
Then just start your progress dialog in your onCreate, and stop it when your map is initialized.

Community
  • 1
  • 1
David Seroussi
  • 1,650
  • 2
  • 17
  • 34
-1

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 () {
    }
    @Override
    public Dialog onCreateDialog(final Bundle savedInstanceState) {
        mDialog = new ProgressDialog( getActivity());
        this.setStyle( STYLE_NO_TITLE, getTheme()); 
        mDialog.setMessage("Building the map ... "); 
        mDialog.setCancelable(false);
        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 ();
myInstance.show( fm, "some_tag");
new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground( Void... voids ) {
        try { Thread.sleep( 300); } catch( Exception e) {}
        return null;
    }
    @Override
    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();
    }
}.execute(); 
tm1701
  • 7,307
  • 17
  • 79
  • 168