Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to combine an EditTextPreference with a CheckBoxPreference?

I have a PreferenceActivity with, among other things, a category including call forward options. What I want is a preference that:

  • Enables/Disables if the user presses a checkbox on the right.
  • Opens up the EditTextPreference dialog if the user presses the text(or anything else in the preference)

It's probably not of any use but here is a snippet of this particular preferencecategory :

    <PreferenceCategory
        android:title="@string/category_callforward">

    <EditTextPreference
            android:key="call_forward_always"
            android:title="@string/call_forward_always"
            android:summary="@string/call_forward_forwardto" />
    </PreferenceCategory>

EDIT

I'd like to implement it in this method if possible:

    // Locates the correct data from saved preferences and sets input type to numerics only
private void setCallForwardType()
{
    ep1 = (EditTextPreference) findPreference("call_forward_always");

    EditText et = (EditText) ep1.getEditText();
    et.setKeyListener(DigitsKeyListener.getInstance());

}

EDIT2

If anyone is still wondering - this is what I want as a Preference:

http://i.imgur.com/GHOeK.png

EDIT3

I've searched around for a couple hours now and have come up with a single word: 'PreferenceGroupAdapter'. I have not, however, been able to find examples or tutorials showing me how to use it. Suggestions ? Is this even the correct path to go?

EDIT4

If this really isn't possibly I would very much like a suggestion to an alternative(user-friendly) solution that I can implement instead of the combined Edit- and Checkbox preference.

like image 973
CodePrimate Avatar asked Mar 16 '12 08:03

CodePrimate


3 Answers

You can do this. First, create a class for preferences which should be extended from PreferenceActivity. Use like this:

// editbox ise your EditTextPreference, so set it.  
checkbox = (CheckBoxPreference) findPreference("checkbox_preference");

checkbox.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if(newValue.toString().equals("false")) {
            PrefActivity.this.editbox.setEnabled(false);
        } else if(newValue.toString().equals("true")) {
            PrefActivity.this.editbox.setEnabled(true);
        }
        return true;
    }
});

I hope it helps.

like image 56
Ogulcan Orhan Avatar answered Nov 07 '22 21:11

Ogulcan Orhan


A bit late but I think I've managed to create something similar with a dialog that creates a layout with an edit text and a checkbox, it should be possible to do the same in a normal layout:

 public class CheckEditTextPreference extends DialogPreference {
    private static final String KEY_PROPERTY_DISABLED = "key_property_disabled";
    private EditText editText;
    private CheckBox checkBox;
    private String text;
    private boolean isDisabled;
    private SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());

    public CheckEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected View onCreateDialogView() {
        return buildUi();
    }

    /**
     * Build a dialog using an EditText and a CheckBox
     */
    private View buildUi() {

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(25, 0, 0, 0);

        LinearLayout linearLayout = new LinearLayout(getContext());
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(layoutParams);

        checkBox = new CheckBox(getContext());
        editText = new EditText(getContext());

        editText.setLayoutParams(layoutParams);
        checkBox.setLayoutParams(layoutParams);
        checkBox.setText("Disabled");

        FrameLayout dialogView = new FrameLayout(getContext());
        linearLayout.addView(editText);
        linearLayout.addView(checkBox);
        dialogView.addView(linearLayout);

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editText.setEnabled(!isChecked);
            }
        });

        return dialogView;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        checkBox.setChecked(isDisabled());
        editText.setText(getText());
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if (positiveResult) {
            String text = editText.getText().toString();
            boolean isChecked = checkBox.isChecked();
            if (callChangeListener(text)) {
                setText(text);
            }

            if (callChangeListener(isChecked)) {
                isDisabled(isChecked);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return a.getString(index);
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        setText(restorePersistedValue ? getPersistedString("") : defaultValue.toString());
        isDisabled(mySharedPreferences.getBoolean(KEY_PROPERTY_DISABLED, true));
    }

    public void setText(String value) {
        this.text = value;
        persistString(this.text);
    }

    public String getText() {
        return this.text;
    }

    private void isDisabled(boolean value) {
        this.isDisabled = value;
        mySharedPreferences.edit().putBoolean(KEY_PROPERTY_DISABLED, this.isDisabled).apply();
    }

    public boolean isDisabled() {
        return this.isDisabled;
    }
}

And put this into your preferences screen:

    <your.package.name.CheckEditTextPreference
        android:key="chkEtPref"
        android:title="Title"/>

Disabled Enabled

like image 2
Abdelilah El Aissaoui Avatar answered Nov 07 '22 21:11

Abdelilah El Aissaoui


Define a key in res/values/strings.xml for your CheckBoxPreference.

Give your CheckBoxPreference the XML attribute android:key="@string/THE_KEY_YOU_DEFINED" so that it will automatically save state in SharedPreferences.

Give your EditTextPreference the XML attribute android:dependency="@string/THE_KEY_YOU_DEFINED.

The EditTextPreference should then enable / disable depending on the state of the CheckBoxPreference.

like image 1
theblang Avatar answered Nov 07 '22 21:11

theblang