2

Trying to write a custom dialog preference (a NumberPickerDialog). Although the android documentation details on this topic, I seem to miss some essential building blocks in their documentation.

What I have so far is a custom preference dialog that shows up in the settings activity. I can click on the preference and fill in a value and press OK/Cancel.

The custom layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editTextNumberPickerValue"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

NumberPickerDialog (work in progress...):

public class NumberPickerPreference extends DialogPreference {

    public NumberPickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        setDialogLayoutResource(R.layout.numberpicker_dialog);  
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
        setDialogIcon(null);
        setPersistent(false);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            Editor editor = getEditor();
            persistInt( value??? );
        }
    }
}

Preference.xml extended with:

<com.cleancode.utils.numberpickerpreference.NumberPickerPreference
    android:defaultValue="550"
    android:key="prefLongFlashDuration"
    android:summary="@string/long_flash_duration_summary"
    android:title="@string/long_flash_duration" />

How can I:

  • actually show the default value of 550?
  • retrieve the value from the dialog?
  • enforce that only integer values are entered?

I hope somebody can shed some light on this, pitty Android documentation is not a little newby friendly on this topic.

Many thanks.

bas
  • 13,550
  • 20
  • 69
  • 146
  • The link below answers the question https://stackoverflow.com/a/18601126/1151799 – arungiri_10 Jan 18 '18 at 13:27
  • That answer relies on `findIndexOfValue(getValue())` to retrieve the selected value. That method is not available in a `DialogPreference` since it wouldn't make sense – Hilikus May 10 '18 at 02:40

1 Answers1

0

On Google's doc they say this about the variable that contains the new value

In this example, mNewValue is a class member that holds the setting's current value.

So it seems you have to rely on a member field to keep track of the view that contains your new value. This is the way I solved it

private EditText newPasswordField;

@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);

    newPasswordField = view.findViewById(R.id.new_password_field);
}

So basically you override a method that will let you keep a reference to whatever element holds your new setting's value

And then you do what you already had: extract the new value

@Override
protected void onDialogClosed(boolean positiveResult) {
    Log.i(TAG, "Password dialog closed. Result = " + positiveResult);

    if (positiveResult) {
        persistString(newPasswordField.getText().toString());
    }
}
Hilikus
  • 9,954
  • 14
  • 65
  • 118