3

I have an EditTextPreference in the PreferenceActivity. When user click the EditTextPreference will show a dialog. In the dialog, user can input a value, and the dialog has "OK" and "Cancel" buttons. I want to call the click event of ok button to check the value, but I do not know how to call the click even.

I know I can use EditTextPreference.setOnPreferenceChangeListener(), but I want to know if I can use OK button click event.

Uniruddh
  • 4,427
  • 3
  • 52
  • 86
pengwang
  • 19,536
  • 34
  • 119
  • 168
  • It is possible by creating your custom EditTextPreference. Check this out: http://stackoverflow.com/a/15219411/2107118 – appsroxcom Mar 05 '13 at 08:57

3 Answers3

4

You can extend EditTextPreference to get control over the click handler.

package myPackage;
public class CustomEditTextPreference extends EditTextPreference {

    public CustomEditTextPreference(Context context) {
        super(context);
    }

    public CustomEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (which == DialogInterface.BUTTON_POSITIVE) {
            // add Handler here
        }
        super.onClick(dialog, which);
    }

}

In the Xml instead of <EditTextPreference/> reference it like this:

<myPackage.CustomEditTextPreference android:dialogTitle="Registration Key" android:key="challengeKey" android:title="Registration Key" android:summary="Click here to enter the registration key you received by email."/>
tmanthey
  • 4,547
  • 6
  • 35
  • 42
  • I first tried this with only 1 constructor and got "Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class". I got it working, so make sure you override all 3 constructors! – wrapperapps May 26 '15 at 15:59
  • 1
    Yes, the constructor called depends on how it gets instantiated (XML or not). To have all options override all 3 constructors. – tmanthey May 27 '15 at 13:42
3

Actually you can't since the preference is using an internal AlertDialog.Builder and creates a new dialog every time you click the preference. The next problem is that the dialog builder sets the click listener for you and if you override them you might destroy the close behavior of the button click.

This bothered me since I wanted a preference which only closes on valid input (otherwise a toast is shown and user should press cancel if he can't get it right).

(If you really need a solution for exactly this problem) You can find general solution of a validating DialogPreference here and a validating EditTextPreference here which I wrote myself.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
Knickedi
  • 8,742
  • 3
  • 43
  • 45
  • @Knickedi the provided links appear to be broken. Is your solution still available somewhere? – THelper Apr 23 '12 at 07:22
  • Thank you for the hint. The project moved to GitHub an you'll find the code there (fixed the links). – Knickedi Apr 23 '12 at 19:29
  • I really hate the Android Preference API. I am constantly running into situations that aren't handled by it, that I would think are pretty common. – theblang Feb 06 '14 at 20:26
1

Your preference activity doesn't appear to be implementing a

OnSharedPreferenceChangeListener

You may want to read over the excellent answer to the question: Updating EditPreference

Community
  • 1
  • 1
coder_For_Life22
  • 26,645
  • 20
  • 86
  • 118
  • 1
    thankyou i used the OnSharedPreferenceChangeListener to solve my problem,but @Knickedi answer is more practice,so i accive his answer,i am also give you 1 vote.thank you – pengwang Sep 23 '11 at 12:07