1
votes

I am new to programming and trying to create an android app using phonegap. I am using the dialogs plugin in cordova 3.5 (Phonegap Dialog Plugin), to enable a prompt, to get some value from user and store it in a input area.

navigator.notification.prompt(
            'Enter Value : ',  // message
            onPrompt,                  // callback to invoke
            'Input',            // title
            ['Cancel','Options','Ok'],             // buttonLabels
            text               // defaultText
        )

The Default text is the value previously entered by the user. If I want to correct a typo, I have to type everything again. What should I do so that default text isn't overwritten and I can edit the default text from the current value itself?

Found this link: http://twigstechtips.blogspot.in/2011/10/android-allow-user-to-editinput-text.html

So, i tried editing this file: /plugins/org.apache.cordova.dialogs/src/android/Notifications.java

Original code:

public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels, final String defaultText, final CallbackContext callbackContext) {

    final CordovaInterface cordova = this.cordova;

    Runnable runnable = new Runnable() {
        public void run() {
            final EditText promptInput =  new EditText(cordova.getActivity());
            promptInput.setHint(defaultText);
            AlertDialog.Builder dlg = new AlertDialog.Builder(cordova.getActivity());
            dlg.setMessage(message);
            dlg.setTitle(title);
            dlg.setCancelable(true);

            dlg.setView(promptInput);

            final JSONObject result = new JSONObject();

            // First button
            if (buttonLabels.length() > 0) {
                try {
                    dlg.setNegativeButton(buttonLabels.getString(0),
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                try {
                                    result.put("buttonIndex",1);
                                    result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());                                            
                                } catch (JSONException e) { e.printStackTrace(); }
                                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                            }
                        });
                } catch (JSONException e) { }
            }

            // Second button
            if (buttonLabels.length() > 1) {
                try {
                    dlg.setNeutralButton(buttonLabels.getString(1),
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                try {
                                    result.put("buttonIndex",2);
                                    result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
                                } catch (JSONException e) { e.printStackTrace(); }
                                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                            }
                        });
                } catch (JSONException e) { }
            }

            // Third button
            if (buttonLabels.length() > 2) {
                try {
                    dlg.setPositiveButton(buttonLabels.getString(2),
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                try {
                                    result.put("buttonIndex",3);
                                    result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
                                } catch (JSONException e) { e.printStackTrace(); }
                                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                            }
                        });
                } catch (JSONException e) { }
            }
            dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {
                public void onCancel(DialogInterface dialog){
                    dialog.dismiss();
                    try {
                        result.put("buttonIndex",0);
                        result.put("input1", promptInput.getText().toString().trim().length()==0 ? defaultText : promptInput.getText());
                    } catch (JSONException e) { e.printStackTrace(); }
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, result));
                }
            });

            dlg.create();
            dlg.show();

        };
    };
    this.cordova.getActivity().runOnUiThread(runnable);
}

Made one change:

          final EditText promptInput =  new EditText(this);
promptInput.setHint(defaultText);

and second change:

 result.put("input1", promptInput.getText().toString().trim().length()==0 ? "" : promptInput.getText());                                            
                                } 

But doesn't work. The default text can't be edited from the current value, everything gets overwritten.

What changes do i need to make, to get the desired functionality?

2
what you are asking for is very vague.. if you are sending the text from the prompt somewhere then its going to get replace there too, so from what im understanding you want the previously entered string to remain there forever so the user can make changes. - allwynmasc
I get the same issue on Android, but on iOS it's possible to modify the default text. - user276648

2 Answers

0
votes

I think I got it.

You just have to declare the var defaultText global with window and assign the entered value in the onPrompt callback. Try and see if this is it:

window.defaultText = "enter ur stuff";
        var showMyPrompt = function showPrompt() {                
                navigator.notification.prompt(
                'Please enter your name',  // message
                onPrompt,                  // callback to invoke
                'Registration',            // title
                ['Ok','Exit'],             // buttonLabels
                defaultText                 // defaultText
            );              
        };

     // process the promptation dialog result
function onPrompt(results) {
            alert("You selected button number " + results.buttonIndex + " and entered " + results.input1);
            defaultText = results.input1;
        }

So every time what you add to the prompt remains when you again tap the prompt! No need of high tech js editing.

0
votes

I had the same issue on Android and simply replaced

promptInput.setHint(defaultText);

by

promptInput.setText(defaultText);