Example usage for android.app AlertDialog.Builder setOnCancelListener

List of usage examples for android.app AlertDialog.Builder setOnCancelListener

Introduction

In this page you can find the example usage for android.app AlertDialog.Builder setOnCancelListener.

Prototype

public void setOnCancelListener(@Nullable OnCancelListener listener) 

Source Link

Document

Set a listener to be invoked when the dialog is canceled.

Usage

From source file:net.granoeste.scaffold.app.ScaffoldAlertDialogFragment.java

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    int iconId = getArguments().getInt(ICON_ID, 0);
    String title = getArguments().getString(TITLE);
    String message = getArguments().getString(MESSAGE);
    boolean hasPositive = getArguments().getBoolean(HAS_POSITIVE, false);
    boolean hasNeutral = getArguments().getBoolean(HAS_NEUTRAL, false);
    boolean hasNegative = getArguments().getBoolean(HAS_NEGATIVE, false);
    String positiveText = getArguments().getString(POSITIVE_TEXT);
    String neutralText = getArguments().getString(NEUTRAL_TEXT);
    String negativeText = getArguments().getString(NEGATIVE_TEXT);
    boolean cancelable = getArguments().getBoolean(CANCELABLE, true);
    boolean canceledOnTouchOutside = getArguments().getBoolean(CANCELED_ON_TOUCH_OUTSIDE, false);

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    if (iconId > 0) {
        builder.setIcon(iconId);/*from  www .j  a v  a2 s  . c  o m*/
    }
    if (StringUtils.isNoneEmpty(title)) {
        builder.setTitle(title);
    }
    if (StringUtils.isNoneEmpty(message)) {
        builder.setMessage(message);
    }
    if (hasPositive) {
        if (StringUtils.isEmpty(positiveText)) {
            positiveText = getResources().getString(R.string.yes);
        }
        builder.setPositiveButton(positiveText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final int whichButton) {
                synchronized (mOnAlertDialogEventListeners) {
                    for (OnAlertDialogEventListener listener : mOnAlertDialogEventListeners) {
                        listener.onDialogClick(dialog, whichButton, getTag());
                    }
                }
            }
        });
    }
    if (hasNeutral) {
        if (StringUtils.isEmpty(neutralText)) {
            neutralText = getResources().getString(R.string.no);
        }
        builder.setNeutralButton(neutralText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final int whichButton) {
                synchronized (mOnAlertDialogEventListeners) {
                    for (OnAlertDialogEventListener listener : mOnAlertDialogEventListeners) {
                        listener.onDialogClick(dialog, whichButton, getTag());
                    }
                }
            }
        });
    }
    if (hasNegative) {
        if (StringUtils.isEmpty(negativeText)) {
            negativeText = getResources().getString(hasNeutral ? R.string.cancel : R.string.no);
        }
        builder.setNegativeButton(negativeText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final int whichButton) {
                synchronized (mOnAlertDialogEventListeners) {
                    for (OnAlertDialogEventListener listener : mOnAlertDialogEventListeners) {
                        listener.onDialogClick(dialog, whichButton, getTag());
                    }
                }
            }
        });
    }
    builder.setCancelable(cancelable);
    if (cancelable) {
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(final DialogInterface dialog) {
                synchronized (mOnAlertDialogEventListeners) {
                    for (OnAlertDialogEventListener listener : mOnAlertDialogEventListeners) {
                        listener.onDialogCancel(dialog, getTag());
                    }
                }
            }
        });
    }
    //        View customView = getCustomView();
    if (mCustomView != null) {
        builder.setView(mCustomView);
    }

    Dialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);

    return dialog;
}

From source file:org.kde.necessitas.ministro.MinistroActivity.java

private void checkNetworkAndDownload(final boolean update) {
    if (isOnline(this))
        new CheckLibraries().execute(update);
    else {//  w w w.j a  v a 2 s . c  o m
        AlertDialog.Builder builder = new AlertDialog.Builder(MinistroActivity.this);
        builder.setMessage(getResources().getString(R.string.ministro_network_access_msg));
        builder.setCancelable(true);
        builder.setNeutralButton(getResources().getString(R.string.settings_msg),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        final ProgressDialog m_dialog = ProgressDialog.show(MinistroActivity.this, null,
                                getResources().getString(R.string.wait_for_network_connection_msg), true, true,
                                new DialogInterface.OnCancelListener() {
                                    public void onCancel(DialogInterface dialog) {
                                        finishMe();
                                    }
                                });
                        getApplication().registerReceiver(new BroadcastReceiver() {
                            @Override
                            public void onReceive(Context context, Intent intent) {
                                if (isOnline(MinistroActivity.this)) {
                                    try {
                                        getApplication().unregisterReceiver(this);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    runOnUiThread(new Runnable() {
                                        public void run() {
                                            m_dialog.dismiss();
                                            new CheckLibraries().execute(update);
                                        }
                                    });
                                }
                            }
                        }, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
                        try {
                            startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
                        } catch (Exception e) {
                            e.printStackTrace();
                            try {
                                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                            } catch (Exception e1) {

                                e1.printStackTrace();
                            }
                        }
                        dialog.dismiss();
                    }
                });
        builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface dialog) {
                finishMe();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

From source file:org.apache.cordova.core.Notification.java

/**
 * Builds and shows a native Android prompt dialog with given title, message, buttons.
 * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
 * The following results are returned to the JavaScript callback identified by callbackId:
 *     buttonIndex         Index number of the button selected
 *     input1            The text entered in the prompt dialog box
 *
 * @param message           The message the dialog should display
 * @param title             The title of the dialog
 * @param buttonLabels      A comma separated list of button labels (Up to 3 buttons)
 * @param callbackContext   The callback context.
 *//*ww w . j  av  a  2s .c o  m*/
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels,
        final String defaultText, final CallbackContext callbackContext) {

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

    Runnable runnable = new Runnable() {
        public void run() {
            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);
}

From source file:edu.cmu.cylab.starslinger.view.HomeActivity.java

protected AlertDialog.Builder xshowChangeSenderOptions(final Activity act, Bundle args) {
    final ArrayList<UseContactItem> items = new ArrayList<UseContactItem>();
    boolean isContactInUse = args.getBoolean(extra.CREATED);
    final String pName = args.getString(extra.NAME);
    byte[] pPhoto = args.getByteArray(extra.PHOTO);
    String pLookupKey = args.getString(extra.CONTACT_LOOKUP_KEY);
    if (!TextUtils.isEmpty(pName)) {
        items.add(new UseContactItem(String.format(act.getString(R.string.menu_UseProfilePerson), pName),
                pPhoto, pLookupKey, UCType.PROFILE));
    }//ww w. j  a  v  a  2s  .  com
    int i = 0;
    String cName = args.getString(extra.NAME + i);
    byte[] cPhoto = args.getByteArray(extra.PHOTO + i);
    String cLookupKey = args.getString(extra.CONTACT_LOOKUP_KEY + i);
    while (!TextUtils.isEmpty(cName)) {
        items.add(new UseContactItem(String.format(act.getString(R.string.menu_UseContactPerson), cName),
                cPhoto, cLookupKey, UCType.CONTACT));
        i++;
        cName = args.getString(extra.NAME + i);
        cPhoto = args.getByteArray(extra.PHOTO + i);
        cLookupKey = args.getString(extra.CONTACT_LOOKUP_KEY + i);
    }
    items.add(new UseContactItem(act.getString(R.string.menu_UseNoContact), UCType.NONE));
    items.add(new UseContactItem(act.getString(R.string.menu_UseAnother), UCType.ANOTHER));
    items.add(new UseContactItem(act.getString(R.string.menu_CreateNew), UCType.NEW));
    items.add(new UseContactItem(act.getString(R.string.menu_EditName), UCType.EDIT_NAME));
    if (isContactInUse) {
        items.add(new UseContactItem(act.getString(R.string.menu_EditContact), UCType.EDIT_CONTACT));
    }

    AlertDialog.Builder ad = new AlertDialog.Builder(act);
    ad.setTitle(R.string.title_MyIdentity);
    ad.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            dialog.dismiss();
        }
    });
    ListAdapter adapter = new ArrayAdapter<UseContactItem>(act, android.R.layout.select_dialog_item,
            android.R.id.text1, items) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView) v.findViewById(android.R.id.text1);
            UseContactItem item = items.get(position);

            if (item.contact) {
                Drawable d;
                if (item.icon != null) {
                    Bitmap bm = BitmapFactory.decodeByteArray(item.icon, 0, item.icon.length, null);
                    d = new BitmapDrawable(getResources(), bm);
                } else {
                    d = getResources().getDrawable(R.drawable.ic_silhouette);
                }
                int avatar_size_list = (int) getResources().getDimension(R.dimen.avatar_size_list);
                d.setBounds(0, 0, avatar_size_list, avatar_size_list);
                tv.setCompoundDrawables(null, null, d, null);
                tv.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.size_5dp));
            } else {
                tv.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
            }

            return v;
        }
    };
    ad.setAdapter(adapter, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            dialog.dismiss();
            switch (items.get(item).type) {
            case PROFILE:
                // user wants to use found profile as a personal contact
                // save these for lookup and display purposes
                SafeSlingerPrefs.setContactName(pName);
                SafeSlingerPrefs.setContactLookupKey(items.get(item).contactLookupKey);
                refreshView();
                break;
            case CONTACT:
                // user wants to use found contact as a personal contact
                SafeSlingerPrefs.setContactName(getContactName(items.get(item).contactLookupKey));
                SafeSlingerPrefs.setContactLookupKey(items.get(item).contactLookupKey);
                refreshView();
                break;
            case ANOTHER:
                // user wants to choose new contact for themselves
                showPickContact(RESULT_PICK_CONTACT_SENDER);
                break;
            case NONE:
                // user wants to remove link to address book
                SafeSlingerPrefs.setContactLookupKey(null);
                refreshView();
                break;
            case NEW:
                // user wants to create new contact
                showAddContact(SafeSlingerPrefs.getContactName());
                break;
            case EDIT_CONTACT:
                // user wants to edit contact
                showEditContact(RESULT_PICK_CONTACT_SENDER);
                break;
            case EDIT_NAME:
                // user wants to edit name
                showSettings();
                refreshView();
                break;
            }
        }
    });

    return ad;
}

From source file:org.apache.cordova.dialogs.Notification.java

/**
 * Builds and shows a native Android prompt dialog with given title, message, buttons.
 * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
 * The following results are returned to the JavaScript callback identified by callbackId:
 *     buttonIndex         Index number of the button selected
 *     input1            The text entered in the prompt dialog box
 *
 * @param message           The message the dialog should display
 * @param title             The title of the dialog
 * @param buttonLabels      A comma separated list of button labels (Up to 3 buttons)
 * @param callbackContext   The callback context.
 *//* w w  w . jav a 2 s . c  o  m*/
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 = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            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));
                }
            });

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

From source file:jp.watnow.plugins.dialog.Notification.java

/**
 * Builds and shows a native Android prompt dialog with given title, message, buttons.
 * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
 * The following results are returned to the JavaScript callback identified by callbackId:
 *     buttonIndex         Index number of the button selected
 *     input1            The text entered in the prompt dialog box
 *
 * @param message           The message the dialog should display
 * @param title             The title of the dialog
 * @param buttonLabels      A comma separated list of button labels (Up to 3 buttons)
 * @param callbackContext   The callback context.
 *//*from w w  w. ja  v a2  s  .  co m*/
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels,
        final String defaultText, final String dialogType, 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);
            Log.d("DialogPlugin", dialogType);
            if (dialogType.equals(INPUT_SECURE)) {
                promptInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
            AlertDialog.Builder dlg = createDialog(cordova); // new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            dlg.setMessage(message);
            dlg.setTitle(title);
            dlg.setCancelable(false);

            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));
                }
            });

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

From source file:com.remobile.dialogs.Notification.java

/**
 * Builds and shows a native Android prompt dialog with given title, message, buttons.
 * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
 * The following results are returned to the JavaScript callback identified by callbackId:
 * buttonIndex         Index number of the button selected
 * input1            The text entered in the prompt dialog box
 *
 * @param message         The message the dialog should display
 * @param title           The title of the dialog
 * @param buttonLabels    A comma separated list of button labels (Up to 3 buttons)
 * @param callbackContext The callback context.
 *//*from w w w .j  av  a2  s .  co  m*/
public synchronized void prompt(final String message, final String title, final JSONArray buttonLabels,
        final String defaultText, final CallbackContext callbackContext) {
    final Activity activity = this.cordova.getActivity();
    Runnable runnable = new Runnable() {
        public void run() {
            final EditText promptInput = new EditText(activity);
            promptInput.setHint(defaultText);
            AlertDialog.Builder dlg = createDialog(); // new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            dlg.setMessage(message);
            dlg.setTitle(title);
            dlg.setCancelable(false);

            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));
                }
            });

            changeTextDirection(dlg);
        }

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

From source file:com.owncloud.android.ui.activity.ReceiveExternalFilesActivity.java

@Override
protected Dialog onCreateDialog(final int id) {
    final AlertDialog.Builder builder = new Builder(this);
    switch (id) {
    case DIALOG_NO_ACCOUNT:
        builder.setIcon(R.drawable.ic_warning);
        builder.setTitle(R.string.uploader_wrn_no_account_title);
        builder.setMessage(/*from w  w  w.ja  v  a 2  s  . com*/
                String.format(getString(R.string.uploader_wrn_no_account_text), getString(R.string.app_name)));
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                    // using string value since in API7 this
                    // constatn is not defined
                    // in API7 < this constatant is defined in
                    // Settings.ADD_ACCOUNT_SETTINGS
                    // and Settings.EXTRA_AUTHORITIES
                    Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
                    intent.putExtra("authorities", new String[] { MainApp.getAuthTokenType() });
                    startActivityForResult(intent, REQUEST_CODE__SETUP_ACCOUNT);
                } else {
                    // since in API7 there is no direct call for
                    // account setup, so we need to
                    // show our own AccountSetupAcricity, get
                    // desired results and setup
                    // everything for ourself
                    Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class);
                    startActivityForResult(intent, REQUEST_CODE__SETUP_ACCOUNT);
                }
            }
        });
        builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        return builder.create();
    case DIALOG_MULTIPLE_ACCOUNT:
        Account accounts[] = mAccountManager.getAccountsByType(MainApp.getAccountType());
        CharSequence dialogItems[] = new CharSequence[accounts.length];
        OwnCloudAccount oca;
        for (int i = 0; i < dialogItems.length; ++i) {
            try {
                oca = new OwnCloudAccount(accounts[i], this);
                dialogItems[i] = oca.getDisplayName() + " @ " + DisplayUtils
                        .convertIdn(accounts[i].name.substring(accounts[i].name.lastIndexOf("@") + 1), false);

            } catch (Exception e) {
                Log_OC.w(TAG, "Couldn't read display name of account; using account name instead");
                dialogItems[i] = DisplayUtils.convertIdn(accounts[i].name, false);
            }
        }
        builder.setTitle(R.string.common_choose_account);
        builder.setItems(dialogItems, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                setAccount(mAccountManager.getAccountsByType(MainApp.getAccountType())[which]);
                onAccountSet(mAccountWasRestored);
                dialog.dismiss();
                mAccountSelected = true;
                mAccountSelectionShowing = false;
            }
        });
        builder.setCancelable(true);
        builder.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                mAccountSelectionShowing = false;
                dialog.cancel();
                finish();
            }
        });
        return builder.create();
    default:
        throw new IllegalArgumentException("Unknown dialog id: " + id);
    }
}

From source file:org.petero.droidfish.DroidFish.java

private final Dialog networkEngineDialog() {
    String[] fileNames = findFilesInDirectory(engineDir, new FileNameFilter() {
        @Override/*from   w ww . j av a2s . c  o  m*/
        public boolean accept(String filename) {
            if (reservedEngineName(filename))
                return false;
            return EngineUtil.isNetEngine(filename);
        }
    });
    final int numFiles = fileNames.length;
    final int numItems = numFiles + 1;
    final String[] items = new String[numItems];
    final String[] ids = new String[numItems];
    int idx = 0;
    String sep = File.separator;
    String base = Environment.getExternalStorageDirectory() + sep + engineDir + sep;
    for (int i = 0; i < numFiles; i++) {
        ids[idx] = base + fileNames[i];
        items[idx] = fileNames[i];
        idx++;
    }
    ids[idx] = "";
    items[idx] = getString(R.string.new_engine);
    idx++;
    String currEngine = ctrl.getEngine();
    int defaultItem = 0;
    for (int i = 0; i < numItems; i++)
        if (ids[i].equals(currEngine)) {
            defaultItem = i;
            break;
        }
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.configure_network_engine);
    builder.setSingleChoiceItems(items, defaultItem, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            if ((item < 0) || (item >= numItems))
                return;
            dialog.dismiss();
            if (item == numItems - 1) {
                showDialog(NEW_NETWORK_ENGINE_DIALOG);
            } else {
                networkEngineToConfig = ids[item];
                removeDialog(NETWORK_ENGINE_CONFIG_DIALOG);
                showDialog(NETWORK_ENGINE_CONFIG_DIALOG);
            }
        }
    });
    builder.setOnCancelListener(new Dialog.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            removeDialog(MANAGE_ENGINES_DIALOG);
            showDialog(MANAGE_ENGINES_DIALOG);
        }
    });
    AlertDialog alert = builder.create();
    return alert;
}

From source file:com.owncloud.android.ui.activity.Uploader.java

@Override
protected Dialog onCreateDialog(final int id) {
    final AlertDialog.Builder builder = new Builder(this);
    switch (id) {
    case DIALOG_WAITING:
        final ProgressDialog pDialog = new ProgressDialog(this, R.style.ProgressDialogTheme);
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);/*from  w w  w  .  j  a  va  2 s.co  m*/
        pDialog.setMessage(getResources().getString(R.string.uploader_info_uploading));
        pDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                ProgressBar v = (ProgressBar) pDialog.findViewById(android.R.id.progress);
                v.getIndeterminateDrawable().setColorFilter(getResources().getColor(R.color.color_accent),
                        android.graphics.PorterDuff.Mode.MULTIPLY);

            }
        });
        return pDialog;
    case DIALOG_NO_ACCOUNT:
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(R.string.uploader_wrn_no_account_title);
        builder.setMessage(
                String.format(getString(R.string.uploader_wrn_no_account_text), getString(R.string.app_name)));
        builder.setCancelable(false);
        builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                    // using string value since in API7 this
                    // constatn is not defined
                    // in API7 < this constatant is defined in
                    // Settings.ADD_ACCOUNT_SETTINGS
                    // and Settings.EXTRA_AUTHORITIES
                    Intent intent = new Intent(android.provider.Settings.ACTION_ADD_ACCOUNT);
                    intent.putExtra("authorities", new String[] { MainApp.getAuthTokenType() });
                    startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
                } else {
                    // since in API7 there is no direct call for
                    // account setup, so we need to
                    // show our own AccountSetupAcricity, get
                    // desired results and setup
                    // everything for ourself
                    Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class);
                    startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
                }
            }
        });
        builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        return builder.create();
    case DIALOG_MULTIPLE_ACCOUNT:
        CharSequence ac[] = new CharSequence[mAccountManager
                .getAccountsByType(MainApp.getAccountType()).length];
        for (int i = 0; i < ac.length; ++i) {
            ac[i] = DisplayUtils.convertIdn(mAccountManager.getAccountsByType(MainApp.getAccountType())[i].name,
                    false);
        }
        builder.setTitle(R.string.common_choose_account);
        builder.setItems(ac, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                setAccount(mAccountManager.getAccountsByType(MainApp.getAccountType())[which]);
                onAccountSet(mAccountWasRestored);
                dialog.dismiss();
                mAccountSelected = true;
                mAccountSelectionShowing = false;
            }
        });
        builder.setCancelable(true);
        builder.setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                mAccountSelectionShowing = false;
                dialog.cancel();
                finish();
            }
        });
        return builder.create();
    case DIALOG_NO_STREAM:
        builder.setIcon(android.R.drawable.ic_dialog_alert);
        builder.setTitle(R.string.uploader_wrn_no_content_title);
        builder.setMessage(R.string.uploader_wrn_no_content_text);
        builder.setCancelable(false);
        builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        return builder.create();
    default:
        throw new IllegalArgumentException("Unknown dialog id: " + id);
    }
}