Example usage for android.app AlertDialog setOnDismissListener

List of usage examples for android.app AlertDialog setOnDismissListener

Introduction

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

Prototype

public void setOnDismissListener(@Nullable OnDismissListener listener) 

Source Link

Document

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

Usage

From source file:edu.usf.cutr.opentripplanner.android.fragments.MainFragment.java

@Override
public void onOTPGeocodingComplete(final boolean isStartTextbox, ArrayList<Address> addressesReturn,
        boolean geocodingForMarker) {
    if (getActivity() != null) {
        removeFocus(isStartTextbox);/*from   w  w w.  ja v a 2 s  .c  o m*/

        boolean geocodingWasRequested = mIsStartLocationGeocodingBeenRequested
                || mDestinationGeocodingBeenRequested;

        if (isStartTextbox) {
            mIsStartLocationGeocodingCompleted = true;
            mIsStartLocationGeocodingBeenRequested = false;
        } else {
            mIsEndLocationGeocodingCompleted = true;
            mDestinationGeocodingBeenRequested = false;
        }

        try {
            AlertDialog.Builder geocoderAlert = new AlertDialog.Builder(getActivity());
            geocoderAlert.setTitle(R.string.geocoder_results_title)
                    .setMessage(R.string.geocoder_results_no_results_message).setCancelable(false)
                    .setPositiveButton(getResources().getString(android.R.string.ok),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                }
                            });

            if (addressesReturn.isEmpty()) {
                restartTextBoxLocation(isStartTextbox);
                AlertDialog alert = geocoderAlert.create();
                alert.show();
                return;
            } else if (addressesReturn.size() == 1) {
                if (geocodingForMarker) {
                    moveMarkerRelative(isStartTextbox, addressesReturn.get(0));
                } else {
                    moveMarker(isStartTextbox, addressesReturn.get(0));
                }
                if (geocodingWasRequested) {
                    requestTripAfterGeocoding();
                }
                return;
            }

            AlertDialog.Builder geocoderSelector = new AlertDialog.Builder(getActivity());
            geocoderSelector.setTitle(R.string.geocoder_results_title);

            final CharSequence[] addressesText = new CharSequence[addressesReturn.size()];
            for (int i = 0; i < addressesReturn.size(); i++) {
                Address address = addressesReturn.get(i);
                addressesText[i] = getStringAddress(address, true);

                Log.v(OTPApp.TAG, addressesText[i].toString());
            }

            final ArrayList<Address> addressesTemp = addressesReturn;
            geocoderSelector.setItems(addressesText, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Address address = addressesTemp.get(item);
                    moveMarker(isStartTextbox, address);
                    Log.v(OTPApp.TAG, "Chosen: " + addressesText[item]);
                    MainFragment.this.requestTripAfterGeocoding();
                }
            });
            AlertDialog alertGeocoder = geocoderSelector.create();
            alertGeocoder.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    restartTextBoxLocation(isStartTextbox);
                }
            });
            alertGeocoder.show();
        } catch (Exception e) {
            Log.e(OTPApp.TAG, "Error in Main Fragment Geocoding callback: " + e);
        }
    }
}

From source file:com.mantz_it.rfanalyzer.MainActivity.java

/**
 * Will pop up a dialog to let the user adjust gain settings
 *//*from   w  w w . j  a  v  a2  s  . c  o  m*/
private void adjustGain() {
    if (source == null)
        return;

    int sourceType = Integer.valueOf(preferences.getString(getString(R.string.pref_sourceType), "1"));
    switch (sourceType) {
    case FILE_SOURCE:
        Toast.makeText(this, getString(R.string.filesource_doesnt_support_gain), Toast.LENGTH_LONG).show();
        break;
    case HACKRF_SOURCE:
        // Prepare layout:
        final LinearLayout view_hackrf = (LinearLayout) this.getLayoutInflater().inflate(R.layout.hackrf_gain,
                null);
        final SeekBar sb_hackrf_vga = (SeekBar) view_hackrf.findViewById(R.id.sb_hackrf_vga_gain);
        final SeekBar sb_hackrf_lna = (SeekBar) view_hackrf.findViewById(R.id.sb_hackrf_lna_gain);
        final TextView tv_hackrf_vga = (TextView) view_hackrf.findViewById(R.id.tv_hackrf_vga_gain);
        final TextView tv_hackrf_lna = (TextView) view_hackrf.findViewById(R.id.tv_hackrf_lna_gain);
        sb_hackrf_vga.setMax(HackrfSource.MAX_VGA_RX_GAIN / HackrfSource.VGA_RX_GAIN_STEP_SIZE);
        sb_hackrf_lna.setMax(HackrfSource.MAX_LNA_GAIN / HackrfSource.LNA_GAIN_STEP_SIZE);
        sb_hackrf_vga.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_hackrf_vga.setText("" + progress * HackrfSource.VGA_RX_GAIN_STEP_SIZE);
                ((HackrfSource) source).setVgaRxGain(progress * HackrfSource.VGA_RX_GAIN_STEP_SIZE);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb_hackrf_lna.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_hackrf_lna.setText("" + progress * HackrfSource.LNA_GAIN_STEP_SIZE);
                ((HackrfSource) source).setLnaGain(progress * HackrfSource.LNA_GAIN_STEP_SIZE);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb_hackrf_vga.setProgress(((HackrfSource) source).getVgaRxGain() / HackrfSource.VGA_RX_GAIN_STEP_SIZE);
        sb_hackrf_lna.setProgress(((HackrfSource) source).getLnaGain() / HackrfSource.LNA_GAIN_STEP_SIZE);

        // Show dialog:
        AlertDialog hackrfDialog = new AlertDialog.Builder(this).setTitle("Adjust Gain Settings")
                .setView(view_hackrf).setPositiveButton("Set", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // safe preferences:
                        SharedPreferences.Editor edit = preferences.edit();
                        edit.putInt(getString(R.string.pref_hackrf_vgaRxGain),
                                sb_hackrf_vga.getProgress() * HackrfSource.VGA_RX_GAIN_STEP_SIZE);
                        edit.putInt(getString(R.string.pref_hackrf_lnaGain),
                                sb_hackrf_lna.getProgress() * HackrfSource.LNA_GAIN_STEP_SIZE);
                        edit.apply();
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // do nothing
                    }
                }).create();
        hackrfDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                // sync source with (new/old) settings
                int vgaRxGain = preferences.getInt(getString(R.string.pref_hackrf_vgaRxGain),
                        HackrfSource.MAX_VGA_RX_GAIN / 2);
                int lnaGain = preferences.getInt(getString(R.string.pref_hackrf_lnaGain),
                        HackrfSource.MAX_LNA_GAIN / 2);
                if (((HackrfSource) source).getVgaRxGain() != vgaRxGain)
                    ((HackrfSource) source).setVgaRxGain(vgaRxGain);
                if (((HackrfSource) source).getLnaGain() != lnaGain)
                    ((HackrfSource) source).setLnaGain(lnaGain);
            }
        });
        hackrfDialog.show();
        hackrfDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        break;
    case RTLSDR_SOURCE:
        final int[] possibleGainValues = ((RtlsdrSource) source).getPossibleGainValues();
        final int[] possibleIFGainValues = ((RtlsdrSource) source).getPossibleIFGainValues();
        if (possibleGainValues.length <= 1 && possibleIFGainValues.length <= 1) {
            Toast.makeText(MainActivity.this, source.getName() + " does not support gain adjustment!",
                    Toast.LENGTH_LONG).show();
        }
        // Prepare layout:
        final LinearLayout view_rtlsdr = (LinearLayout) this.getLayoutInflater().inflate(R.layout.rtlsdr_gain,
                null);
        final LinearLayout ll_rtlsdr_gain = (LinearLayout) view_rtlsdr.findViewById(R.id.ll_rtlsdr_gain);
        final LinearLayout ll_rtlsdr_ifgain = (LinearLayout) view_rtlsdr.findViewById(R.id.ll_rtlsdr_ifgain);
        final Switch sw_rtlsdr_manual_gain = (Switch) view_rtlsdr.findViewById(R.id.sw_rtlsdr_manual_gain);
        final CheckBox cb_rtlsdr_agc = (CheckBox) view_rtlsdr.findViewById(R.id.cb_rtlsdr_agc);
        final SeekBar sb_rtlsdr_gain = (SeekBar) view_rtlsdr.findViewById(R.id.sb_rtlsdr_gain);
        final SeekBar sb_rtlsdr_ifGain = (SeekBar) view_rtlsdr.findViewById(R.id.sb_rtlsdr_ifgain);
        final TextView tv_rtlsdr_gain = (TextView) view_rtlsdr.findViewById(R.id.tv_rtlsdr_gain);
        final TextView tv_rtlsdr_ifGain = (TextView) view_rtlsdr.findViewById(R.id.tv_rtlsdr_ifgain);

        // Assign current gain:
        int gainIndex = 0;
        int ifGainIndex = 0;
        for (int i = 0; i < possibleGainValues.length; i++) {
            if (((RtlsdrSource) source).getGain() == possibleGainValues[i]) {
                gainIndex = i;
                break;
            }
        }
        for (int i = 0; i < possibleIFGainValues.length; i++) {
            if (((RtlsdrSource) source).getIFGain() == possibleIFGainValues[i]) {
                ifGainIndex = i;
                break;
            }
        }
        sb_rtlsdr_gain.setMax(possibleGainValues.length - 1);
        sb_rtlsdr_ifGain.setMax(possibleIFGainValues.length - 1);
        sb_rtlsdr_gain.setProgress(gainIndex);
        sb_rtlsdr_ifGain.setProgress(ifGainIndex);
        tv_rtlsdr_gain.setText("" + possibleGainValues[gainIndex]);
        tv_rtlsdr_ifGain.setText("" + possibleIFGainValues[ifGainIndex]);

        // Assign current manual gain and agc setting
        sw_rtlsdr_manual_gain.setChecked(((RtlsdrSource) source).isManualGain());
        cb_rtlsdr_agc.setChecked(((RtlsdrSource) source).isAutomaticGainControl());

        // Add listener to gui elements:
        sw_rtlsdr_manual_gain.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                sb_rtlsdr_gain.setEnabled(isChecked);
                tv_rtlsdr_gain.setEnabled(isChecked);
                sb_rtlsdr_ifGain.setEnabled(isChecked);
                tv_rtlsdr_ifGain.setEnabled(isChecked);
                ((RtlsdrSource) source).setManualGain(isChecked);
                if (isChecked) {
                    ((RtlsdrSource) source).setGain(possibleGainValues[sb_rtlsdr_gain.getProgress()]);
                    ((RtlsdrSource) source).setIFGain(possibleIFGainValues[sb_rtlsdr_ifGain.getProgress()]);
                }
            }
        });
        cb_rtlsdr_agc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ((RtlsdrSource) source).setAutomaticGainControl(isChecked);
            }
        });
        sb_rtlsdr_gain.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_rtlsdr_gain.setText("" + possibleGainValues[progress]);
                ((RtlsdrSource) source).setGain(possibleGainValues[progress]);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        sb_rtlsdr_ifGain.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_rtlsdr_ifGain.setText("" + possibleIFGainValues[progress]);
                ((RtlsdrSource) source).setIFGain(possibleIFGainValues[progress]);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        // Disable gui elements if gain cannot be adjusted:
        if (possibleGainValues.length <= 1)
            ll_rtlsdr_gain.setVisibility(View.GONE);
        if (possibleIFGainValues.length <= 1)
            ll_rtlsdr_ifgain.setVisibility(View.GONE);

        if (!sw_rtlsdr_manual_gain.isChecked()) {
            sb_rtlsdr_gain.setEnabled(false);
            tv_rtlsdr_gain.setEnabled(false);
            sb_rtlsdr_ifGain.setEnabled(false);
            tv_rtlsdr_ifGain.setEnabled(false);
        }

        // Show dialog:
        AlertDialog rtlsdrDialog = new AlertDialog.Builder(this).setTitle("Adjust Gain Settings")
                .setView(view_rtlsdr).setPositiveButton("Set", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // safe preferences:
                        SharedPreferences.Editor edit = preferences.edit();
                        edit.putBoolean(getString(R.string.pref_rtlsdr_manual_gain),
                                sw_rtlsdr_manual_gain.isChecked());
                        edit.putBoolean(getString(R.string.pref_rtlsdr_agc), cb_rtlsdr_agc.isChecked());
                        edit.putInt(getString(R.string.pref_rtlsdr_gain),
                                possibleGainValues[sb_rtlsdr_gain.getProgress()]);
                        edit.putInt(getString(R.string.pref_rtlsdr_ifGain),
                                possibleIFGainValues[sb_rtlsdr_ifGain.getProgress()]);
                        edit.apply();
                    }
                }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // do nothing
                    }
                }).create();
        rtlsdrDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                boolean manualGain = preferences.getBoolean(getString(R.string.pref_rtlsdr_manual_gain), false);
                boolean agc = preferences.getBoolean(getString(R.string.pref_rtlsdr_agc), false);
                int gain = preferences.getInt(getString(R.string.pref_rtlsdr_gain), 0);
                int ifGain = preferences.getInt(getString(R.string.pref_rtlsdr_ifGain), 0);
                ((RtlsdrSource) source).setGain(gain);
                ((RtlsdrSource) source).setIFGain(ifGain);
                ((RtlsdrSource) source).setManualGain(manualGain);
                ((RtlsdrSource) source).setAutomaticGainControl(agc);
                if (manualGain) {
                    // Note: This is a workaround. After setting manual gain to true we must
                    // rewrite the manual gain values:
                    ((RtlsdrSource) source).setGain(gain);
                    ((RtlsdrSource) source).setIFGain(ifGain);
                }
            }
        });
        rtlsdrDialog.show();
        rtlsdrDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        break;
    default:
        Log.e(LOGTAG, "adjustGain: Invalid source type: " + sourceType);
        break;
    }
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

private void showSearchResultDialog(final List<SearchResult> results) {

    if (isSearchResultsDialogShowing) {
        return;/* w w  w .jav  a2s. co  m*/
    }

    isSearchResultsDialogShowing = true;

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(R.string.search_results);

    SearchResultAdapter adapter = new SearchResultAdapter(context, bookView, results);
    builder.setAdapter(adapter, adapter);

    AlertDialog dialog = builder.create();
    dialog.setOwnerActivity(getActivity());
    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            isSearchResultsDialogShowing = false;
        }
    });
    dialog.show();
}

From source file:no.barentswatch.fiskinfo.BaseActivity.java

/**
 * This function creates a pop up dialog which takes in the context of the
 * current activity//from  ww w  .  j ava 2s. c  om
 * 
 * @param activityContext
 *            the context of the current activity
 * @param rPathToTitleOfPopup
 *            The R.path to the title
 * @param customView
 *            the custom view for the dialog
 */
public void ShowLoginDialog(Context activityContext, int rPathToTitleOfPopup, View customView) {
    LayoutInflater layoutInflater = getLayoutInflater();
    View view = layoutInflater.inflate(R.layout.dialog_login, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(activityContext);
    builder.setTitle(rPathToTitleOfPopup);

    final EditText usernameEditText = (EditText) view.findViewById(R.id.LoginDialogEmailField);
    final EditText passwordEditText = (EditText) view.findViewById(R.id.loginDialogPasswordField);
    final TextView incorrectCredentialsTextView = (TextView) view
            .findViewById(R.id.loginIncorrectCredentialsTextView);

    Button loginButton = (Button) view.findViewById(R.id.loginDialogButton);
    Button cancelButton = (Button) view.findViewById(R.id.cancel_login_button);
    builder.setView(view);
    final AlertDialog dialog = builder.create();
    dialog.setCanceledOnTouchOutside(false);

    loginButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (incorrectCredentialsTextView.getVisibility() == 0) {
                incorrectCredentialsTextView.setVisibility(android.view.View.INVISIBLE);
            }

            if (!isNetworkAvailable()) {
                incorrectCredentialsTextView.setText(getString(R.string.no_internet_access));
                incorrectCredentialsTextView.setVisibility(android.view.View.VISIBLE);
                return;
            }

            String usernameText = usernameEditText.getText().toString();
            String passwordText = passwordEditText.getText().toString();

            if (!validateEmail(usernameText)) {
                usernameEditText.requestFocus();
                usernameEditText.setError(getString(R.string.login_invalid_email));
                return;
            }
            usernameEditText.setError(null);

            if (passwordText.length() == 0) {
                passwordEditText.requestFocus();
                passwordEditText.setError(getString(R.string.login_password_field_empty_string));
                return;
            }
            passwordEditText.setError(null);

            try {
                authenticateUserCredentials(usernameText, passwordText);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (userIsAuthenticated) {
                loadView(MyPageActivity.class);
            } else {
                incorrectCredentialsTextView.setText(getString(R.string.login_incorrect_credentials));
                incorrectCredentialsTextView.setVisibility(android.view.View.VISIBLE);
                return;
            }
        }
    });

    cancelButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();

        }
    });

    dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            // TODO Auto-generated method stub
            actionBar.setSelectedNavigationItem(adapter.getCount());
        }
    });

    dialog.show();
}

From source file:org.mozilla.gecko.GeckoPreferences.java

protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    AlertDialog dialog = null;
    switch (id) {
    case DIALOG_CREATE_MASTER_PASSWORD:
        final EditText input1 = getTextBox(R.string.masterpassword_password);
        final EditText input2 = getTextBox(R.string.masterpassword_confirm);
        linearLayout.addView(input1);//  ww  w.j  av a  2 s .c  o m
        linearLayout.addView(input2);

        builder.setTitle(R.string.masterpassword_create_title).setView((View) linearLayout)
                .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        JSONObject jsonPref = new JSONObject();
                        try {
                            jsonPref.put("name", "privacy.masterpassword.enabled");
                            jsonPref.put("type", "string");
                            jsonPref.put("value", input1.getText().toString());

                            GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:Set",
                                    jsonPref.toString());
                            GeckoAppShell.sendEventToGecko(event);
                        } catch (Exception ex) {
                            Log.e(LOGTAG, "Error setting masterpassword", ex);
                        }
                        return;
                    }
                }).setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
        dialog = builder.create();
        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            public void onShow(DialogInterface dialog) {
                input1.setText("");
                input2.setText("");
                input1.requestFocus();
            }
        });

        PasswordTextWatcher watcher = new PasswordTextWatcher(input1, input2, dialog);
        input1.addTextChangedListener((TextWatcher) watcher);
        input2.addTextChangedListener((TextWatcher) watcher);

        break;
    case DIALOG_REMOVE_MASTER_PASSWORD:
        final EditText input = getTextBox(R.string.masterpassword_password);
        linearLayout.addView(input);

        builder.setTitle(R.string.masterpassword_remove_title).setView((View) linearLayout)
                .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            JSONObject jsonPref = new JSONObject();
                            jsonPref.put("name", "privacy.masterpassword.enabled");
                            jsonPref.put("type", "string");
                            jsonPref.put("value", input.getText().toString());

                            GeckoEvent event = GeckoEvent.createBroadcastEvent("Preferences:Set",
                                    jsonPref.toString());
                            GeckoAppShell.sendEventToGecko(event);
                        } catch (Exception ex) {
                            Log.e(LOGTAG, "Error setting masterpassword", ex);
                        }
                        return;
                    }
                }).setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
        dialog = builder.create();
        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            public void onDismiss(DialogInterface dialog) {
                input.setText("");
            }
        });
        break;
    default:
        return null;
    }

    return dialog;
}