Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created November 3, 2011 15:43
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ChrisMcKee/1336815 to your computer and use it in GitHub Desktop.
Save ChrisMcKee/1336815 to your computer and use it in GitHub Desktop.
Loading Dialog Phonegap Plugin
if(!window.plugins) {
window.plugins = {};
}
window.plugins.loadingIndicator = {
show: function(options, callback){
window.PhoneGap.exec(
callback,
null,
'LoadingIndicator',
'showLoading',
[options]
);
},
hide: function(callback){
window.PhoneGap.exec(
callback,
null,
'LoadingIndicator',
'hideLoading',
[]
);
}
};
package com.phonegap.plugins; /*or just use your own namespace*/
import org.json.JSONArray;
import android.app.ProgressDialog;
import android.util.Log;
import com.phonegap.DroidGap;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult;
import com.phonegap.api.PluginResult.Status;
public class PGLoadingDialog extends Plugin {
final static String TAG = PGLoadingDialog.class.getSimpleName();
static PGLoadingDialog instance = null;
static ProgressDialog dialog = null;
public static final String LOADING = "showLoading";
public static final String FINISHEDLOADING = "hideLoading";
public PGLoadingDialog() {
instance = this;
}
public static PGLoadingDialog getInstance() {
return instance;
}
private Thread t;
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {
PluginResult result = new PluginResult(Status.INVALID_ACTION);
final DroidGap droidGap = (DroidGap)this.ctx;
if(LOADING.contains(action)){
Log.i(TAG, "------ Spin ON");
t = new Thread(new Runnable() {
public void run() {
ctx.runOnUiThread(new Runnable() {
public void run() {
droidGap.spinnerStart("", "Loading. Please wait...");
}
});
}
});
t.start();
result = new PluginResult(Status.OK);
}
else if(FINISHEDLOADING.contains(action)){
Log.i(TAG, "------ Spin OFF");
droidGap.spinnerStop();
t.stop();
result = new PluginResult(Status.OK);
}
return new PluginResult(Status.OK);
}
}
<!-- Add this into /res/xml/plugins.xml -->
<plugin name="LoadingIndicator" value=" com.phonegap.plugins.PGLoadingDialog" />
@ppstlineage
Copy link

how to write call plugin in javascript ?

@strfle
Copy link

strfle commented Feb 16, 2013

Thanks! I updated this for cordova 2.1.0

//Show it

Indicator.show("",show); 
      var show = function(error){

   };

//Hide it

    Indicator.hide("",hide); 
        var hide = function(error){
        };

//JS

var Indicator = {

    show: function(options, callback){

        window.Cordova.exec(
            callback, 
            null, 
            'LoadingIndicator', 
            'showLoading', 
            [options]
        );

    },


    hide: function(callback){

        window.Cordova.exec(
            callback, 
            null, 
            'LoadingIndicator', 
            'hideLoading', 
            []
        );

    }

};

//JAVA

package com.foregroundgalleryplugin;

import org.json.JSONArray;

import android.app.ProgressDialog;
import android.util.Log;

import org.apache.cordova.DroidGap;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.PluginResult.Status;

public class PGLoadingDialog extends Plugin {

final static String TAG = PGLoadingDialog.class.getSimpleName();

static PGLoadingDialog instance = null;
static ProgressDialog dialog = null;


public static final String LOADING = "showLoading";
public static final String FINISHEDLOADING = "hideLoading";

public PGLoadingDialog() {
    instance = this;
}

public static PGLoadingDialog getInstance() {
    return instance;
}

private Thread t;

@SuppressWarnings("deprecation")
@Override
public PluginResult execute(String action, JSONArray data, String callbackId) {

    @SuppressWarnings("unused")
    PluginResult result = new PluginResult(Status.INVALID_ACTION);
    final DroidGap droidGap = (DroidGap)this.cordova.getContext();

    if(LOADING.contains(action)){
        Log.i(TAG, "------ Spin ON");

        t = new Thread(new Runnable() { 
            @SuppressWarnings("deprecation")
            public void run() { 
                ctx.runOnUiThread(new Runnable() { 
                    public void run() { 
                        droidGap.spinnerStart("", "Loading. Please wait..."); 
                    } 
                }); 
            } 
        }); 

        t.start(); 

        result = new PluginResult(Status.OK);
    }
    else if(FINISHEDLOADING.contains(action)){

        Log.i(TAG, "------ Spin OFF");
        droidGap.spinnerStop();
        t.stop();
        result = new PluginResult(Status.OK);
    }


    return  new PluginResult(Status.OK);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment