Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:44:33+00:00 2026-06-15T15:44:33+00:00

I have a PreferenceScreen in which the user is capable, if system can’t autodetect

  • 0

I have a PreferenceScreen in which the user is capable, if system can’t autodetect it, to enter the device’s phone number. I’m still learning this part of Android but I managed to understand a bit of PreferenceScreens by examples provided by Android SDK itself and a few tutorials.

What I want is that the user can save the phone number only if null or valid, where by “valid” I mean running a generic validation logic (ie. an anonymous method that returns true or false, that can be reused in any possible situation*) or better, just to simplify things, ^(\+39)?3[0-9]{9}$

For now I have the following XML snip

<EditTextPreference
    android:inputType="phone"
    android:key="@string/preference_phoneNo"
    android:selectAllOnFocus="true"
    android:singleLine="true"
    android:summary="@string/pref_phoneNumber_description"
    android:title="@string/pref_phoneNumber" />

and following code by courtesy of Eclipse New Activity wizard:

private void setupSimplePreferencesScreen() {
    if (!isSimplePreferences(this)) {
        return;
    }

    addPreferencesFromResource(R.xml.pref_general);
    bindPreferenceSummaryToValue(findPreference(getString(R.string.preference_phoneNo)));

}

addPreferenceFromResource is supposed to load the XML node and add the preference to the screen, while binPreferenceSummaryToValue is supposed to make description text change when preference is updated. Just for sake of completeness for those who don’t like code courtesy of the IDE, the second method is provided by Eclipse who also provides a private class in the code file that is

     /**
 * A preference value change listener that updates the preference's summary
 * to reflect its new value.
 */

In the general case, what should I do to perform validation logic before the preference gets saved when I click OK on the preference editor? Where is the validation logic to be put in a PreferenceScreen?

*Aren’t we all here to learn?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:44:34+00:00Added an answer on June 15, 2026 at 3:44 pm

    Android has a built in helper method for this.

    String phoneNumber = ...;
    boolean valid = PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber);
    

    For a generic, re-usable method, here’s the implementation of that method in PhoneNumberUtils, courtesy of AOSP (Apache licensed)

     private static final Pattern GLOBAL_PHONE_NUMBER_PATTERN =
             Pattern.compile("[\\+]?[0-9.-]+");
     ...
     public static boolean isGlobalPhoneNumber(String phoneNumber) {
         if (TextUtils.isEmpty(phoneNumber)) {
             return false;
         }
    
         Matcher match = GLOBAL_PHONE_NUMBER_PATTERN.matcher(phoneNumber);
         return match.matches();
     }
    

    Validation should occur within a Preference.OnPreferenceChangeListener , in the onPreferenceChange method. Simply return false if you don’t want the value to be saved.

    Example snippet:

    private static Preference.OnPreferenceChangeListener myListener =
        new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object value) {
                String stringValue = value.toString();
                if (preference instanceof PhoneNumberPreference) {
                    return isGlobalPhoneNumber(value);
                }
            }
            ...
    

    As a note, since you’re starting off with the Settings activity generated by the Eclipse wizard, this listener has already been built for you. You just need to edit it to include validation of the phone number (assuming that’s what’s being edited), and to return false if the number is invalid, so it won’t be saved to preferences.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PreferenceScreen, where the user can check/uncheck a checkbox. To get access
I have a working user-preferences screen which is defined in user_prefs.xml. The xml is
I am refering to http://developer.android.com/reference/android/app/Activity.html . I have an activity which can be interrupted
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
I'm currently writing a live wallpaper for Android and it has a PreferenceScreen which
I have created a Custom preference which has the following constructor public CoordinatesPreference(Context context,
I have PreferenceScreen with some PreferenceCheckBoxes. I want to change the textColor and textSize
I have a OnPreferenceClickListener which should remove a specific Preference (with key preference_to_remove )

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.