@Override
  protected void showDialog(Bundle state) {
    if (getEntries() == null || getEntryValues() == null) {
      throw new IllegalStateException(
          "ListPreference requires an entries array and an entryValues array.");
    }

    int preselect = findIndexOfValue(getValue());
    AlertDialog.Builder builder =
        new AlertDialog.Builder(getContext())
            .setTitle(getDialogTitle())
            .setIcon(getDialogIcon())
            .setSingleChoiceItems(
                getEntries(),
                preselect,
                new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    if (which >= 0 && getEntryValues() != null) {
                      String value = getEntryValues()[which].toString();
                      if (callChangeListener(value) && isPersistent()) setValue(value);
                    }
                    dialog.dismiss();
                  }
                });

    PreferenceManager pm = getPreferenceManager();
    try {
      Method method =
          pm.getClass()
              .getDeclaredMethod(
                  "registerOnActivityDestroyListener",
                  PreferenceManager.OnActivityDestroyListener.class);
      method.setAccessible(true);
      method.invoke(pm, this);
    } catch (Exception e) {
      e.printStackTrace();
    }

    mDialog = builder.create();
    if (state != null) mDialog.onRestoreInstanceState(state);
    mDialog.show();
  }
Ejemplo n.º 2
0
  @SuppressWarnings("ConstantConditions")
  public void searchRadiusDialog() {
    scanValue = Integer.valueOf(preferences.getString(SettingsUtil.SCAN_VALUE, "4"));

    final AppCompatDialog dialog = new AppCompatDialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_search_radius);

    final SeekBar seekBar = (SeekBar) dialog.findViewById(R.id.seekBar);
    Button btnSave = (Button) dialog.findViewById(R.id.btnAccept);
    Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
    final TextView tvNumber = (TextView) dialog.findViewById(R.id.tvNumber);
    final TextView tvEstimate = (TextView) dialog.findViewById(R.id.tvEstimate);
    tvNumber.setText(String.valueOf(scanValue));
    tvEstimate.setText(
        getString(R.string.timeEstimate) + " " + UiUtils.getSearchTime(scanValue, this));
    seekBar.setProgress(scanValue);
    seekBar.setMax(12);
    seekBar.setOnSeekBarChangeListener(
        new SeekBar.OnSeekBarChangeListener() {
          @Override
          public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            tvNumber.setText(String.valueOf(i));
            tvEstimate.setText(
                getString(R.string.timeEstimate)
                    + " "
                    + UiUtils.getSearchTime(i, SettingsActivity.this));
          }

          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {}

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

    btnSave.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            int saveValue = seekBar.getProgress();
            if (saveValue == 0) {
              scanValue = 1;
            } else {
              scanValue = saveValue;
            }
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(SettingsUtil.SCAN_VALUE, String.valueOf(scanValue));
            editor.apply();
            dialog.dismiss();
          }
        });

    btnCancel.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View view) {
            dialog.dismiss();
          }
        });
    dialog.show();
  }
 @Override
 public void onActivityDestroy() {
   super.onActivityDestroy();
   if (mDialog != null && mDialog.isShowing()) mDialog.dismiss();
 }