Search code examples
androidandroid-preferences

Let the user choose from a range of numbers in preference screen


I would like the user to be able to enter/pick a number in between 300 and 2000. I tought of useing EditTextPreference but then I wouldn't know how to check if the value is actually in range. i'm very new to preference screens.

My Preferences.java

public class Preferences extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.simple);
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        SettingsFragment settingsFragment = new SettingsFragment();
        fragmentTransaction.add(android.R.id.content, settingsFragment, "SETTINGS_FRAGMENT");
        fragmentTransaction.commit();

    }


    public static class SettingsFragment extends PreferenceFragment{

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

            addPreferencesFromResource(R.xml.pref_layout);
        }
    }


}

And the pref_layout.xml (it will only contain one preference)

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="General">

        <EditTextPreference
            android:title="frequency"
            android:summary="frequency"
            android:key="interval"
            android:defaultValue="300"
            android:numeric="integer"/>


    </PreferenceCategory>


</PreferenceScreen>

THANK YOU


Solution

  • What you can do is instead of extending a PreferenceFragment, what you can do is extend a EditTextPreference. And you can override showDialog() and in showDialog() you can add a TextWatcher. Your code should look something like this.

    public static class InputValueEditTextPreference extends EditTextPreference {
    
        public SettingsFragment(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void showDialog(Bundle state) {
            super.showDialog(state);
    
            EditText et = getEditText();
            et.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) {
                    Dialog d = getDialog();
                    int num = Integer.parseInt(s.getText().toString());
                    if (d instanceof AlertDialog) {
                        AlertDialog dialog = (AlertDialog) d;
                        Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
                        // Check the EditText value
                        if (num >= 300 && num <= 2000) {
                            // Enable OK button
                            positiveButton.setEnabled(true);
                        } else {
                            // Disable the button.
                            positiveButton.setEnabled(false);
                        }
                    }
                }
            });
        }
    }
    

    And now in the xml file it should be

    <PreferenceScreen
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    
    <<package name>.InputValueEditTextPreference
        android:title="@string/pref_input_value_label"
        android:key="@string/pref_input_value_key"
        android:defaultValue="@string/pref_input_value_default"
        android:inputType="number"
        android:singleLine="true" />
    

    What this does is it disables the ok Button based on the value inserted into the edit text.

    Now the value which is entered by the user is saved into SharedPrefrence which can be acssed by the key which you have given to the EditText.