0

I have currently a Preference in my application where the user is prompted to enter between 2 and 10 numerical values.

As this feature is only available for power users and beta testers, not for public release, I decided to let them enter a CSV value.

So, in my EditTextPreference, some users will enter: "1;20;30", some other will enter "1;10;10;10;10;10;10;10" etc...

After, in my code, I am just splitting these values to build an array and execute my code. I have to admit that's not very professional, but that just works!

String[] patternString = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("vibPattern", "0;500;500;500;500;500;500;500").split(";");

The main issue is that I would like to check the validity of the String while the user is writing it!

Does some of you have an idea how to achieve that?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • i dint get your requirement. you wanna check string before it is renedered on edittext. is it? – N-JOY May 18 '12 at 08:02
  • Sorry for my english: requirement is I want to check the value of an edittextpreference before the user validate it. For instance "1;2;3;" should be accepted and "a;b;c" or "something" should be refused. – Waza_Be May 18 '12 at 08:05

3 Answers3

0

why not just programmatically add the amount of wished boxes, (for an example) have a spinner(drop down menu), from where you can choose the amount, and then add that amount of edittext with numerical only?

Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
  • That's just a personnal choice.. A spinner + 10 different preferences is, I think, less usable than entering a few values in only one preference – Waza_Be May 18 '12 at 08:06
0

you need to use "addTextChangedListener" and act according to the text that was , well, changed...

if you wish, you can use regular expressions in order to check for validity .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Meanwhile, I found this, but your answer seems correct! http://code.google.com/p/android-smspopup/source/browse/SMSPopup/src/net/everythingandroid/smspopup/preferences/CustomVibrateListPreference.java – Waza_Be May 18 '12 at 08:36
0

In the EditText entry in your layout add the following:

android:inputType="number" android:digits="0123456789,"

Then on the addTextChangeListener implementations you only have to check if a user just entered a bunch of commas.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54