Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText preference validation

I am using Edittextpreference as one of the preference in settings section. I want to validate this edittextpreference when the user enters data to it and clicks ok; before saving it in sharedpreference.

I am trying to do something like this but this saves the preference first I suppose.

editTextPreference
            .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference,
                        Object newValue) {
                    if (((newValue.toString().length() == 15) {
                          // save preference only if length is equal to 15
                     }
              })
               });

can someone guide me how to validate edittextpreference before it is saved in sharedpreference so that I can decide if I want to save it or not.

like image 950
learner Avatar asked Mar 24 '13 03:03

learner


People also ask

How can I validate edittext input in Android?

This example demonstrate about How can I validate EditText input in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.java Step 3 − Add the following code to src/ValidateFilter.java

What is the use of validation in Android?

Validation Validation is a process to ensure that the value entered by the user is within the accepted boundaries of the application. For example, if a user enters a name then the programmer validates the edit text in such a way that it consists of only letters but no numeric values. Step 1 Create a new project. Android.

What is an example of validating the edit text?

For example, if a user enters a name then the programmer validates the edit text in such a way that it consists of only letters but no numeric values. Step 1 Create a new project.

What is edittextpreference?

A DialogPreference that shows a EditText in the dialog. This preference saves a string value. Interface definition for a callback to be invoked when the corresponding dialog view for this preference is bound. A simple Preference.SummaryProvider implementation for an EditTextPreference .


1 Answers

According to doc here

Called when a Preference has been changed by the user. This is called before the state of the Preference is about to be updated and before the state is persisted.

And it returns True to update the state of the Preference with the new value.

So you can do the following

      editTextPreference
            .setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference,
                        Object newValue) {
                    if (((newValue.toString().length() == 15) {
                          //
                          return true;
                     }
                     else{
                          // invalid you can show invalid message
                          return false;
                     }
              })
       });
like image 117
stinepike Avatar answered Oct 19 '22 20:10

stinepike