Example usage for android.app AlertDialog.Builder setCancelable

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

Introduction

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

Prototype

public void setCancelable(boolean flag) 

Source Link

Document

Sets whether this dialog is cancelable with the KeyEvent#KEYCODE_BACK BACK key.

Usage

From source file:app.CT.BTCCalculator.fragments.BreakevenFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Register Bus Provider instance.
    BusProvider.getInstance().register(this);

    View view = getView();//from w  w w  .  j av  a  2  s. c o  m

    // Initialize text fields.
    assert view != null;
    btcBought = (EditText) view.findViewById(R.id.btcBought);
    btcBoughtPrice = (EditText) view.findViewById(R.id.btcBoughtPrice);
    btcSold = (EditText) view.findViewById(R.id.btcSold);
    btcSoldPrice = (EditText) view.findViewById(R.id.btcSoldPrice);
    optimalBTCPrice = (EditText) view.findViewById(R.id.optimalBTCPrice);
    optimalBTC = (EditText) view.findViewById(R.id.optimalBTC);
    resultText = (TextView) view.findViewById(R.id.resultText);

    Button buttonCalculate = (Button) view.findViewById(R.id.buttonCalculate);

    // Checks whether the first visible EditText element is focused in order to enable
    // and show the keyboard to the user. The corresponding XML element has android:imeOptions="actionNext".
    // All EditText elements below are now programmed to show keyboard when pressed.
    btcBought.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    btcBoughtPrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    btcSold.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    btcSoldPrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    optimalBTCPrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    optimalBTC.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    optimalBTCPrice.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            try {
                float result = ((Float.valueOf(btcSold.getText().toString()))
                        * Float.valueOf(btcSoldPrice.getText().toString()))
                        / Float.valueOf(optimalBTCPrice.getText().toString());
                optimalBTC.setText(String.valueOf(result));
            } catch (Exception ignored) {
            }

        }
    });

    btcBoughtPrice.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            containsCurrentRate[0] = false;
        }
    });

    btcSoldPrice.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            containsCurrentRate[1] = false;
        }
    });

    buttonCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            float buyAmount, buyCost, sellAmount, sellPrice, optimalBTCCost, optimalBTCAmount, remainder,
                    totalCost, totalAmount, finalPrice;
            boolean didItWork = true;
            boolean validTransaction = true;

            // Error checking to prevent crashes.
            try {
                // Gets the input entered from the user.
                buyAmount = Float.valueOf(btcBought.getText().toString());
                buyCost = Float.valueOf(btcBoughtPrice.getText().toString());
                sellAmount = Float.valueOf(btcSold.getText().toString());
                sellPrice = Float.valueOf(btcSoldPrice.getText().toString());
                optimalBTCCost = Float.valueOf(optimalBTCPrice.getText().toString());
                optimalBTCAmount = Float.valueOf(optimalBTC.getText().toString());

                // Calculates remainder from the buying and selling.
                remainder = Math.abs((buyAmount - sellAmount));

                // User cannot sell more than they own.
                if (roundTwoDecimals(optimalBTCAmount * optimalBTCCost) > roundTwoDecimals(
                        sellAmount * sellPrice)) {
                    final android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(
                            getActivity());
                    alertDialog.setTitle("Error");
                    alertDialog.setMessage(getString(R.string.optimalBTCErrorMsg));
                    alertDialog.setCancelable(false);
                    alertDialog.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    alertDialog.show();
                    validTransaction = false;
                }

                // Checks if the user typed in a greater selling amount than buying.
                if (sellAmount > buyAmount) {
                    // Create new dialog popup.
                    final android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(
                            getActivity());

                    alertDialog.setTitle("Error");
                    alertDialog.setMessage("You cannot sell more than you own.");
                    alertDialog.setCancelable(false);
                    alertDialog.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // If this button is clicked, close current dialog.
                            dialog.cancel();
                        }
                    });
                    alertDialog.show();

                    // Invalidates transaction since selling amount > buying amount.
                    validTransaction = false;
                }

                // Calculations to output.
                totalCost = buyAmount * buyCost;
                totalAmount = optimalBTCAmount + remainder;
                finalPrice = totalCost / totalAmount;

                if (validTransaction) {
                    resultText.setText(String.format(getString(R.string.resultText),
                            String.valueOf(totalAmount), String.valueOf(roundTwoDecimals(finalPrice))));
                }
            } catch (Exception e) {
                // Sets bool to false in order to execute "finally" block below.
                didItWork = false;
            } finally {
                if (!didItWork) {
                    // Creates new dialog popup.
                    final AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(getActivity());

                    alertDialog2.setTitle("Error");
                    alertDialog2.setMessage("Please fill in all fields.");
                    alertDialog2.setCancelable(false);
                    alertDialog2.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // If this button is clicked, close current dialog.
                            dialog.cancel();
                        }
                    });

                    // Show the dialog.
                    alertDialog2.show();
                }
            }

        }
    });
}

From source file:com.soma.second.matnam.ui.advrecyclerview.fragment.ItemPinnedMessageDialogFragment.java

@NonNull
@Override/*from  w ww  .  jav a 2 s . c o  m*/
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final int itemPosition = getArguments().getInt(KEY_ITEM_POSITION, Integer.MIN_VALUE);

    builder.setMessage(getString(R.string.app_name, itemPosition));
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            notifyItemPinnedDialogDismissed(true);
        }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.setCancelable(true);
    return builder.create();
}

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

/**
 * Builds and shows a native Android confirm dialog with given title, message, buttons.
 * This dialog only shows up to 3 buttons.  Any labels after that will be ignored.
 * The index of the button pressed will be returned to the JavaScript callback identified by callbackId.
 *
 * @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 ww w.j a  va 2 s.c o  m*/
public synchronized void confirm(final String message, final String title, final JSONArray buttonLabels,
        final CallbackContext callbackContext) {
    Runnable runnable = new Runnable() {
        public void run() {
            AlertDialog.Builder dlg = createDialog(); // new AlertDialog.Builder(this.cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
            dlg.setMessage(message);
            dlg.setTitle(title);
            dlg.setCancelable(false);

            // First button
            if (buttonLabels.length() > 0) {
                try {
                    dlg.setNegativeButton(buttonLabels.getString(0), new AlertDialog.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 1));
                        }
                    });
                } 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();
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 2));
                        }
                    });
                } 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();
                            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 3));
                        }
                    });
                } catch (JSONException e) {
                }
            }
            dlg.setOnCancelListener(new AlertDialog.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    dialog.dismiss();
                    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, 0));
                }
            });

            changeTextDirection(dlg);
        }

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

From source file:com.example.chinyao.mow.mow.recycler.ItemPinnedMessageDialogFragment.java

@NonNull
@Override/*  ww  w  .  jav a2 s.c  o m*/
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final int itemPosition = getArguments().getInt(KEY_ITEM_POSITION, Integer.MIN_VALUE);

    builder.setMessage(getString(R.string.dialog_message_item_pinned, itemPosition));
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            notifyItemPinnedDialogDismissed(true);
        }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.setCancelable(true);
    return builder.create();
}

From source file:bulat.diet.helper_couch.common.fragment.ItemPinnedMessageDialogFragment.java

@NonNull
@Override/*from  w ww  .j av a 2 s. c  om*/
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final int itemPosition = getArguments().getInt(KEY_ITEM_POSITION, Integer.MIN_VALUE);

    // builder.setMessage(getString(R.string.dialog_message_item_pinned, itemPosition));
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            notifyItemPinnedDialogDismissed(true);
        }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.setCancelable(true);
    return builder.create();
}

From source file:com.jaspersoft.android.jaspermobile.dialog.RenameDialogFragment.java

@NonNull
@Override/*from  ww w .j a  v a2 s .  co  m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    final View customLayout = LayoutInflater.from(getActivity()).inflate(R.layout.rename_report_dialog_layout,
            null);

    String reportName = FileUtils.getBaseName(selectedFile.getName());
    reportNameEdit = (EditText) customLayout.findViewById(R.id.report_name_input);
    reportNameEdit.setText(reportName);
    reportNameEdit.setSelection(reportNameEdit.getText().length());
    reportNameEdit.addTextChangedListener(new RenameTextWatcher());

    builder.setView(customLayout);
    builder.setTitle(R.string.sdr_rrd_title);
    builder.setCancelable(false);
    builder.setPositiveButton(R.string.ok, null);
    builder.setNegativeButton(R.string.cancel, null);

    mDialog = builder.create();
    mDialog.setOnShowListener(this);
    return mDialog;
}

From source file:com.brayanarias.alarmproject.activity.MainActivity.java

private void setupNavigationView() {
    navigationView = (NavigationView) findViewById(R.id.navigation);
    MenuItem item = navigationView.getMenu().findItem(actualItem);
    View nav_header = LayoutInflater.from(this).inflate(R.layout.nav_header, null);
    final NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
    navigationView.addHeaderView(nav_header);
    //salute/*from   w w w .  j a  v  a2s .  co  m*/
    String salute = AlarmUtilities.getSalute(getApplicationContext());
    TextView tvWellcome = (TextView) nav_header.findViewById(R.id.tvWellcome);
    tvWellcome.setText(salute);
    if (item != null) {
        item.setChecked(true);
    }
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            CharSequence title = menuItem.getTitle();
            closeDrawer();
            switch (menuItem.getItemId()) {
            case R.id.itMyAlarms:
                showFragment(R.id.itMyAlarms);
                actualItem = menuItem.getItemId();
                menuItem.setChecked(true);
                getSupportActionBar().setTitle(title);
                return true;
            case R.id.itAddAlarm:
                Calendar calendar = Calendar.getInstance();
                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                TimePickerDialog timePickerDialog = TimePickerDialog
                        .newInstance(new TimePickerDialog.OnTimeSetListener() {
                            @Override
                            public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) {
                                Alarm alarm = AlarmUtilities.createDefaultAlarm();
                                String min = minute < 10 ? "0" + minute : "" + minute;
                                if (hourOfDay == 12) {
                                    alarm.setAmPm("PM");
                                    alarm.setHour(hourOfDay);
                                    alarm.setHourFormatted((hourOfDay) + ":" + min);
                                } else if (hourOfDay > 12) {
                                    alarm.setAmPm("PM");
                                    alarm.setHour(hourOfDay - 12);
                                    alarm.setHourFormatted((hourOfDay - 12) + ":" + min);
                                } else {
                                    if (hourOfDay == 0) {
                                        hourOfDay = 12;
                                    }
                                    alarm.setAmPm("AM");
                                    alarm.setHour(hourOfDay);
                                    alarm.setHourFormatted(hourOfDay + ":" + min);
                                }
                                alarm.setMinute(minute);
                                Intent intent = new Intent(getApplicationContext(), AddAlarmActivity.class);
                                intent.putExtra(Constant.actionAlarmKey, Constant.addAlarm);
                                intent.putExtra(Constant.alarmSerializableKey, alarm);
                                startActivity(intent);
                            }
                        }, hour, minute, false);
                timePickerDialog.show(getSupportFragmentManager(), "tag");
                return true;
            case R.id.itCurrentMonth:
                actualItem = menuItem.getItemId();
                menuItem.setChecked(true);
                showFragment(R.id.itCurrentMonth);
                getSupportActionBar().setTitle(title);
                return true;
            /*
            case R.id.itConfig:
                actualItem = menuItem.getItemId();
                menuItem.setChecked(true);
                showFragment(R.id.itConfig);
                getSupportActionBar().setTitle(title);
                closeDrawer();
                return true;
            */
            case R.id.itAbout:
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
                WebView wvLegal = (WebView) LayoutInflater.from(getApplication())
                        .inflate(R.layout.web_view_legal, null);
                wvLegal.loadData(getString(R.string.html_legal), "text/html", "UTF-8");
                wvLegal.setWebChromeClient(new WebChromeClient());
                alertBuilder.setView(wvLegal);
                alertBuilder.setTitle(R.string.txt_about_app);
                alertBuilder.setCancelable(true).setPositiveButton(getString(R.string.txt_button_ok),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        });
                alertBuilder.show();
                return true;
            default:
                return false;
            }

        }
    });
}

From source file:com.techno.jay.codingcontests.Home.java

private void promptForUpgrade() {
    AlertDialog.Builder upgradeAlert = new AlertDialog.Builder(Home.this);
    upgradeAlert.setTitle("Please Purchase App plan.");
    upgradeAlert.setCancelable(false);
    upgradeAlert.setMessage("For unlimited use purchase our basic plan.");
    upgradeAlert.setPositiveButton("Purchase", new DialogInterface.OnClickListener() {
        @Override// w w  w .j  a  va2s  .  c o  m
        public void onClick(DialogInterface dialog, int which) {
            //set progress dialog and start the in app purchase process
            // upgradeDialog = ProgressDialog.show(OwnerMenuActivity.this, "Please wait", "Upgrade transaction in process", true);
            bp.purchase(Home.this, Const.Product_Plan_Unlimitedversion);
            dialog.dismiss();
        }
    }).setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            startActivity(new Intent(Home.this, Home.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
            finish();
        }
    });
    upgradeAlert.show();
}

From source file:com.bringcommunications.etherpay.SendActivity.java

private void dsp_txid_and_exit() {
    /*//from   w ww  . j a v  a  2  s  .c  o m
    TextView txid_view = (TextView) findViewById(R.id.txid);
    txid_view.setText(txid);
    if (auto_pay.equals("true")) {
      String msg = txid.isEmpty() ? "transaction failed" : "your transaction was sent successfully";
      Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
      NavUtils.navigateUpFromSameTask(context);
      this.finish();
    } else
    */
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        String title = txid.isEmpty() ? "Error" : "Transaction Sent";
        String msg = txid.isEmpty()
                ? "An error occurred while attempting this transaction -- press OK to continue"
                : "your transaction was sent successfully -- press OK to continue";
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setCancelable(true);
        builder.setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
                NavUtils.navigateUpFromSameTask(context);
                context.finish();
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }
}

From source file:com.example.skode6.scanenvy.MainActivity.java

private AlertDialog enterDialog(final EditText edit) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(this);

    edit.setKeyListener(new DigitsKeyListener());
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
    //edit.setFocusableInTouchMode(true);
    edit.setFocusable(true);/*from   w  w  w  . jav  a 2s. co  m*/
    //edit.setOnClickListener(clickText());
    //edit.requestFocusFromTouch();
    edit.requestFocus();

    dialog.setView(edit);
    dialog.setCancelable(true);
    dialog.setTitle("Enter UPC");
    dialog.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String upc = edit.getText().toString();
            try {
                Product p = run.lookUp(upc);
                adapter.add(p);
                //run.addProduct(p);
            } catch (IOException e) {
                Toast error = Toast.makeText(getApplicationContext(), "Couldn't find" + upc, Toast.LENGTH_LONG);
                error.show();
            }
        }
    });
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    return dialog.create();
}