Validate edittext in PreferenceFragment - android

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>

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

Related

how to display preference values

I'm trying to migrate the preferences part of my app because of warning
default constructor in android.preference.preferenceactivity is deprecated
But, even after reading a lot of ressources, I don't understand why I can't have 2 preference values (string and integer) displayed in front of (or below) the EditTextPreference as I had before.
"Compte" (in the capture) (String) is enabled="false" because I don't want the user can edit it. But I want the user can see the account name in preferences.
"Nom de jours" (Integer) can be edited, if the user click on it, but I want the value is shown without opening editing mode.
prefs.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="#string/prefsCatGlobalTitle">
<EditTextPreference
app:enabled="false"
app:key="param_email"
app:title="#string/cnxEmailLabel" />
</PreferenceCategory>
<PreferenceCategory app:title="#string/prefsCatAlertsTitle">
<EditTextPreference
app:defaultValue="7"
android:inputType="number"
app:key="prefsAlertNbDays"
android:maxLength="2"
app:maxLines="1"
android:selectAllOnFocus="true"
app:singleLine="true"
app:title="#string/prefsAlertNbOfDays" />
<SwitchPreference
app:defaultValue="true"
app:key="prefsAlertNotif"
app:title="#string/prefsAlertNotifTitle" />
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertEmail"
app:title="#string/prefsAlertEmailTitle" />
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertCal"
app:title="#string/prefsAlertCalTitle" />
</PreferenceCategory>
<PreferenceCategory app:title="#string/prefsCatOthersTitle">
<SwitchPreference
app:defaultValue="false"
app:key="prefsAlertAutoSync"
app:title="#string/prefsAlertAutoSync" />
</PreferenceCategory>
</PreferenceScreen>
Preference capture
Apart from the visualization problem, everything works fine and my preferences are well saved and usable in my java code.
I can't find what I'm doing wrong or what I'm missing.
Thanks to #cmak
I never found this app:useSimpleSummaryProvider option before.

how to calculate age from birth date in shared preference

Hello everyone i'm developing android app that takes the birth date of a baby in a preference screen so the app can save it. but what i need after retrieving the value is to calculate the age to use it in different activities in the same app so what can i do and is it possible to convert the string(birth date) into int age??
here is the code for pref.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference
android:title="Enter baby name"
android:key="name"
android:summary="Enter baby name"
android:defaultValue="0"
/>
<EditTextPreference
android:persistent="true"
android:key="age"
android:title="Enter Baby birthday "
android:summary="baby age"
/>
<ListPreference
android:title="Gender"
android:key="list"
android:entries="#array/list"
/>
</PreferenceScreen>
and for getting them
SharedPreferences getPref =
PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String text = getPref.getString("name", " ");
String age = getPref.getString("age", " ");
please help
This kind of library might help: https://github.com/bostonandroid/DatePreference
<org.bostonandroid.datepreference.DatePreference
android:key="age" android:title="#string/babybirthday"
android:defaultValue="1991.01.01" />
Then I guess you are skilled enough to substract 2 Dates ;-)
If not, the link given by Lucifer in comment is very good.

putting a minValue restriction on a preference used for port

I have a preference to get port of a service as shown below
<EditTextPreference
android:defaultValue="4444"
android:key="port"
android:title="Port"
android:dependency="service_on"
android:inputType="number"
android:maxLength="4"
/>
I have restricted it to maxLength 4 but there does not seem to be a minlength or minvalue ??
Basically I want to avoid empty values or lower value ports to avoid conflicts of port already in use/ android allocated ports etc
Any ideas how can I smartly changed the xml only without having to check on callbacks/listeners ?
Thanks,

Android Default Preference xml file format

I appear to be the only person on the internet who can't figure this out, but when I use
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
what is the syntax of the XML file for the default settings. I simply want to set two preferences to true.
I have tried the normal android string resource format, but nothing seems to work. Thanks ahead of time.
Use android:defaultValue like so:
<CheckBoxPreference
android:key="my_pref"
android:defaultValue="true"
android:title="#string/pref_title_my_pref"
android:summary="#string/pref_summary_my_pref"/>
You are not the only person who doesn't know this. Even am struggling to know the file format of the XML. Finally i found the answer! There you go!
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<EditTextPreference android:key="CLIENT_HOMESCREEN_TITLE"
android:defaultValue="Home Screen"/>
<EditTextPreference android:key="CLIENT_ADMIN_BUTTON"
android:defaultValue="Admin"/>
<EditTextPreference android:key="CLIENT_PLAYER_BUTTON"
android:defaultValue="Player"/>
<EditTextPreference android:key="CLIENT_SAVE_BUTTON"
android:defaultValue="Save"/>
<EditTextPreference android:key="CLIENT_CANCEL_BUTTON"
android:defaultValue="Cancel"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_IP"
android:defaultValue="IP:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_PORT"
android:defaultValue="Port:"/>
<EditTextPreference android:key="CLIENT_SERVER_SETUP_TITLE"
android:defaultValue="Server Setup"/>
</PreferenceScreen>
what is the syntax of the XML file for the default settings. I simply want to set two preferences to true.
For this just set default value to true
Whatever default values you have provided in preference xml file, that will be set.

New activity from default preferences

I have setup my default android Preferences with the necessary options. The prime reason of the Preferences are to allow users to manage their resources. In the context of my app, they are "Contacts", "Types of projects" and "Currency".
The "Currency" section works fine. The display just yet, not the programming. But what I really want to do is, when either of the "Contacts" or "Type of projects" is clicked, they should open another custom activity to let the user manage his contacts. These are by the way, not the android contacts. It is an activity connected to a database table allowing users to manage his contacts and his type of projects.
Any advise on this please? Perhaps, I shouldn't be using the default android preferences at all?
This is my XML code for the Preferences:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:title="Manage your resources" >
<ListPreference
android:key="contacts"
android:title="Manage your Contacts"
android:summary="Click here to ADD, EDIT and DELETE Contacts" >
</ListPreference>
<ListPreference
android:key="projects"
android:title="Types of Projects"
android:summary="Click here to maintain a list of Types of Projects that match your profession" >
</ListPreference>
<ListPreference
android:key="currency"
android:title="Select currency"
android:summary="Set a default currency that you wish to use" android:entryValues="#array/entryvalues_list_preference" android:entries="#array/entries_list_preference">
</ListPreference>
</PreferenceCategory>
Thanks in advance....
Perhaps, I shouldn't be using the default android preferences at all?
IMHO, you "shouldn't be using the default android preferences" for "Contacts" and "Types of Projects", if you will be rendering a non-preference UI for them.

Categories

Resources