Q:

Validate edittext in PreferenceFragment

I have an EditTextPreference for the user to enter a Bluetooth MAC address in my preferences.xml file.  I am using android:digits to limit input to valid characters, but that will not prevent someone from adding too little or too many characters, double colons, etc.  If they do these things, the app crashes with an invalid MAC address exception.  Is there a way to do this programmatically?
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="Bluetooth Settings">

        <EditTextPreference
            android:key="prefMacAddress"
            android:title="MAC Address"
            android:lines="1"
            android:maxLines="1"
            android:summary="Enter MAC Address of the Module"
            android:dialogTitle="MAC Address"
            android:dialogMessage="Enter MAC Address of the Module"   
            android:defaultValue="20:13:07:26:24:32"
            android:digits="0123456789abcdef:"/>

    </PreferenceCategory>

</PreferenceScreen>

A:

Use regular expressions for input in your EditText. Here's a good tutorial: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

