Tooroop
Tooroop

Reputation: 1884

Android EditTextPreference on dialog click

I have an EditTextPrefernce in my prefernces fragment. I have set it in XML to be androd:numeric="integer". Ok that's working...but the problem is that the user must insert a value in this preference, he can't delete everything and click ok, because i will get null value then. I have set in the XML default value to be 10000, and in the preference.getLong method to also return a default 1000 value.

So i think i have to somehow check the edit text when the user presses ok in the dialog. Is this possible? or does anyone have a better solution?

Upvotes: 1

Views: 1437

Answers (1)

RPB
RPB

Reputation: 16320

Following is the snippet of the code by which you can achieve what you are looking for,

editPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
                {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue)
                    {
                        try
                        {
                            //Put validation of newValue entered by user
                            //Following is example you can change as you wish
                            if(newValue != null)
                            {
                                long myVal = Long.parseLong(String.valueOf(newValue));
                                if(myVal> 0)
                                {
                                    //All ok
                                }
                                else
                                {
                                   //Invalid Number
                                }
                             }
                        }
                        catch (final Exception e)
                        {
                            return false;
                        }
                        return false;
                    }
                });

Upvotes: 1

Related Questions