Android - No getEditText method in EditTextPreference with Preference Support Library
Asked Answered
S

5

9

I've just had to change to using PreferenceFragmentCompat for implementation of permissions checking which means using the support version of EditTextPreference. Unfortunately it seems getEditText is missing. Is there any work around?

Update Found this issue logged. Seems it's deliberately been removed but the answer doesn't explain how to implement what I want to do.

The code I was using is as follows

final EditText editTextTL;
editTextTL = ((EditTextPreference) findPreference("my_preference")).getEditText();
editTextTL.setFilters(new InputFilter[]{new NumericRangeFilter()});
editTextTL.setOnFocusChangeListener(new AmountOnFocusChangeListener());

My relevant imports

import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceFragmentCompat;

My Gradle dependencies

compile 'com.android.support:support-v4:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:preference-v14:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
Sigmund answered 9/9, 2015 at 10:59 Comment(3)
If you want to use a more powerful (i.e. using XML to set attributes instead of setting from code) version of the accepted solution, I recommend to you to take a look at my workarounds' Interesting things part: github.com/Gericop/Android-Support-Preference-V7-FixKearney
Thanks for the link, I needed to do some stuff that attributes alone wouldn't cope with but the styling elements are going to be useful. Amazing how much is broken with this support lib.Sigmund
I reported a bug for Google, please star it so it can be dealt with : code.google.com/p/android/issues/…Camerlengo
C
2

Create custom EditTextPreference:

public class CustomEditTextPreference extends EditTextPreference {
    public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr,
        int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public CustomEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomEditTextPreference(Context context) {
        super(context);
    }
}

Declare it in xml file for prefences:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <com.example.CustomEditTextPreference
        android:key="test_key"
        android:title="Title" />
</PreferenceScreen>

Update your PreferenceFragmentCompat with the next lines:

public class PrefFragment extends PreferenceFragmentCompat {

private static final String DIALOG_FRAGMENT_TAG =
    "android.support.v7.preference.PreferenceFragment.DIALOG";

@Override
public void onCreatePreferences(Bundle bundle, String s) {
    addPreferencesFromResource(R.xml.settings);
}

@Override
public void onDisplayPreferenceDialog(Preference preference) {
    // check if dialog is already showing
    if (getFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
        return;
    }

    DialogFragment f = null;
    if (preference instanceof CustomEditTextPreference) {
        f = EditTextPreferenceDialog.newInstance(preference.getKey());
    } else {
        super.onDisplayPreferenceDialog(preference);
    }
    if (f != null) {
        f.setTargetFragment(this, 0);
        f.show(getFragmentManager(), DIALOG_FRAGMENT_TAG);
    }
}

public static class EditTextPreferenceDialog extends EditTextPreferenceDialogFragmentCompat {

    public static EditTextPreferenceDialog newInstance(String key) {
        final EditTextPreferenceDialog
            fragment = new EditTextPreferenceDialog();
        final Bundle b = new Bundle(1);
        b.putString(ARG_KEY, key);
        fragment.setArguments(b);
        return fragment;
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        ((EditText)view.findViewById(android.R.id.edit)).setFilters(new InputFilter[]{new NumericRangeFilter()});
    }

}

}

Conjugation answered 9/9, 2015 at 12:17 Comment(4)
There's no getDialog on EditTextPreference within Preference Support Lib either, I was thinking on similar lines myself.Sigmund
I implemented this and can see the init and onFocusChange being called when I debug. But it doesn't get the value from the preference or set it when OK is pressed so my summary/persisted preference isn't updated. Sure something else needs to be implemented but not sure what.Sigmund
@Sigmund I updated the code. Hope it will work for you. I looked into internals of Android and changed the approach. Not a nice solutions, but it works for meConjugation
This works for me. Thanks for looking into this for me so thoroughly. What a mess though for the removal of one simple method from the support library!Sigmund
D
2

Use AndroidX preference library, setOnBindEditTextListener(...), the Listener implementation gives the EditText element, you can customize the EditText as desired.

Demography answered 26/3, 2021 at 7:59 Comment(0)
P
0

Maybe this will help, I was struggling with this today, I found the solution based in fragments. This code works after EditTextPreference click, assuming that preference dialog has been shown.

List<Fragment> fragments = getSupportFragmentManager().getFragments();
Fragment dialogFragment = null;
boolean dialogFragmentFound = false;
for (Fragment f : fragments) {
    if (f != null) {
        String tag = f.getTag();
        if (tag != null) {
            tag = tag.toLowerCase().trim();
            if (tag.indexOf("preference") >= 0 && tag.indexOf("dialog") >= 0) {
                dialogFragmentFound = true;
                dialogFragment = f;
            }
        }
    }
}
if (dialogFragmentFound) {
    Dialog dialog = ((DialogFragment) dialogFragment).getDialog();
    EditText ediText = dialog.findViewById(android.R.id.edit);
    if (ediText != null)
        ediText.setText("your text");
}

Best regards

Psychogenic answered 16/12, 2018 at 19:31 Comment(0)
D
0

2021: Answer

In order to change the EditText value or attributes, you need to set setOnBindEditTextListener callback as per the new AndroidX Kotlin.

findPreference<EditTextPreference>("key")?.setOnBindEditTextListener {
    it.filters = arrayOf<InputFilter>(InputFilter.AllCaps())
}
Domela answered 22/9, 2021 at 19:31 Comment(0)
F
-2

getEditText() is not missing in the support library

check the link https://github.com/consp1racy/android-support-preference/blob/master/library/src/main/java/net/xpece/android/support/preference/EditTextPreference.java

You can get EditText,

please check the version of your support library

Follow answered 9/9, 2015 at 11:17 Comment(2)
I see that library is using android.support.v7.widget.AppCompatEditText; findPreference within PreferenceFragmentCompat cannot cast to that type. I'm using com.android.support:preference-v14:23.0.1Sigmund
This is not the official support library.Kearney

© 2022 - 2024 — McMap. All rights reserved.