2

i try to make own confirm dialog class with boolean value as result. i have read any tutorial or suggestion but the results are not as I want.

The problem is I hate to write long coding, I wanted to write a simple coding. This comparison delphi to android in writing confirmation dialog

Delphi:

if MessageDlg ('Message Text', mtConfirmation, [mbYes, mbNo], 0)

Android:

boolean answer = false;

public boolean Confirm (Activity act, String Title, String ConfirmText,
CancelBtn String, String OkBtn) {
  AlertDialog dialog = new AlertDialog.Builder (act). Create ();
  dialog.setTitle (Title);
  dialog.setMessage (ConfirmText);
  dialog.setCancelable (false);
  dialog.setButton (DialogInterface.BUTTON_POSITIVE, OkBtn,
  new DialogInterface.OnClickListener () {
    public void onClick (DialogInterface dialog, int buttonId) {
  answer = true;
  }
});
dialog.setButton (DialogInterface.BUTTON_NEGATIVE, CancelBtn,
new DialogInterface.OnClickListener () {
  public void onClick (DialogInterface dialog, int buttonId) {
    answer = false;
  }
});
dialog.setIcon (android.R.drawable.ic_dialog_alert);
return answer;
}

the question is can I create a class that generate a confirmation dialogue will produce a Boolean value true or false, just as in Delphi.

confirmation dialog so that the class can be used in class or another activity

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
AsepRoro
  • 353
  • 2
  • 6
  • 19
  • possible duplicate of [How to show a dialog to confirm that the user wishes to exit an Android Activity?](http://stackoverflow.com/questions/2257963/how-to-show-a-dialog-to-confirm-that-the-user-wishes-to-exit-an-android-activity) – PearsonArtPhoto Nov 18 '12 at 16:56
  • I have read that post, but that's not the answer I expected. I want to create a separate class that confirm dialog can be accessed by all the activity with a more efficient coding – AsepRoro Nov 18 '12 at 17:19

4 Answers4

3

Android is not always conventional. You can't show a dialog and wait for it to be displayed, then do something.

You need to show a dialog, and respond to the yes / no buttons in a callback.

Something like this is one solution:

/**
 * Display a confirm dialog. 
 * @param activity
 * @param title
 * @param message
 * @param positiveLabel
 * @param negativeLabel
 * @param onPositiveClick runnable to call (in UI thread) if positive button pressed. Can be null
 * @param onNegativeClick runnable to call (in UI thread) if negative button pressed. Can be null
 */
public static final void confirm(
        final Activity activity, 
        final int title, 
        final int message,
        final int positiveLabel, 
        final int negativeLabel,
        final Runnable onPositiveClick,
        final Runnable onNegativeClick) {

            AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
            dialog.setTitle(title);
            dialog.setMessage(message);
            dialog.setCancelable (false);
            dialog.setPositiveButton(positiveLabel,
                    new DialogInterface.OnClickListener () {
                public void onClick (DialogInterface dialog, int buttonId) {
                    if (onPositiveClick != null) onPositiveClick.run();
                }
            });
            dialog.setNegativeButton(negativeLabel,
                    new DialogInterface.OnClickListener () {
                public void onClick (DialogInterface dialog, int buttonId) {
                    if (onNegativeClick != null) onNegativeClick.run();
                }
            });
            dialog.setIcon (android.R.drawable.ic_dialog_alert);
            dialog.show();

        }

}

To use the above method, something like this can be used:

        Alert.confirm(activity, R.string.titFileExport, R.string.lblFileConfirmOverwrite, 
                R.string.yes, R.string.no, 
                new Runnable() { public void run() {performExport(activity, app, currentSong, saveFile);}}, 
                null);
        return;
ThisIsNotMe
  • 349
  • 2
  • 10
2

You can use custom layout for dialogs, or for some more sophisticated needs it may be much easier to create Activity themed as dialog and call it with startActivityForResults(). Just design you "dialog" UI and theme your activity, by adding this to activity Manifest entry:

android:theme="@android:style/Theme.Dialog"
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

OK. so you want a single method or class that you will just pass values and that will call everytime you need a dialog box. i have made a program that recieve sms and show it in dialog box . you just have to pass values as intent and do whatever you like. its a simple program you can modify as u want. http://zohaibbrohi.blogspot.com/2012/11/recieve-sms-and-show-it-in-activity-as.html

Zohaib Brohi
  • 576
  • 1
  • 7
  • 15
0

try using cordova, the latest stable version is 2.1.0 It has a navigator.notification (alert or confirm in this case) API

http://docs.phonegap.com/en/2.1.0/guide_getting-started_index.md.html#Getting%20Started%20Guides

http://docs.phonegap.com/en/2.1.0/cordova_notification_notification.md.html#Notification The only drawback is that with blackberry, native apps are more error prone