Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - No getEditText method in EditTextPreference with Preference Support Library

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'
like image 507
CodeChimp Avatar asked Sep 09 '15 10:09

CodeChimp


2 Answers

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()});
    }

}

}

like image 95
Ilya Tretyakov Avatar answered Nov 16 '22 20:11

Ilya Tretyakov


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

like image 2
patruni murty Avatar answered Nov 16 '22 22:11

patruni murty