I am trying to find a form validation library in android. Is there such a thing ?
I have a registration form that I want to validate its fields. If the user enters an invalid data, I want to put a red warning mark at the right of the field and pop up a tooltip that he entered an invalid data.
I know about the android:inputType but this is not what I want
I don't know about any such libraries. But if you are working with EditTexts, then your best option is to use a custom TextWatcher:
class TextCheck implements TextWatcher
{
private EditText editor;
public TextCheck(EditText editor)
{
this.editor = editor;
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
// check the text, and if the user entered
// something wrong, change your edittext
if(something wrong)
{
editor.setBackgroundColor(Color.RED); //for example
}
}
#Override
public void afterTextChanged(Editable arg0){}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3){}
}
And then you can use it on all your EditTexts like
EditText editor = (EditText) findViewById(...your id...);
editor.addTextChangedListener(new TextCheck(editor));
Is there such a thing?
Ah.. yes there is one and you can find it here.
It does form validation for you, using but not limited to Annotations. To find out what the library does please visit the following answer on SO where I have described the usage of the library.
If you want to write new rules you can always extend the Rule class.
PS: I am the author of the library.
I know this is old, but you can try this excellent Android Validation library and visit this Stackoverflow reference and this Stackoverflow reference for usage examples because I found the main librarys how-tos kinda of hard to understand.
I did something similar. You can improve this code and adapt for your necessity.
EditTextWithValidation.java
public class EditTextWithValidation extends EditText implements OnTouchListener {
private EditTextValidator mValidator;
public EditTextWithValidation(Context context) {
super(context);
initialize();
}
public EditTextWithValidation(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public EditTextWithValidation(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
public EditTextValidator getCustomValidator() {
return mValidator;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
setError(null);
return false;
}
private void initialize() {
mValidator = new EditTextValidator(this);
setOnTouchListener(this);
}
}
EditTextValidator.java
public class EditTextValidator {
private static final String TAG = EditTextValidator.class.getName();
private enum ValidationResult {
Ok, Rules, Mismatch
}
private EditText mParent;
private Pattern mValidationPattern;
private int mValidationErrorMsgId;
private boolean mAllowEmpty;
private EditText mMatchView;
private int mMismatchMsgId;
private int mMinLength;
private int mMaxLength;
private ValidationResult mValidationResult;
public EditTextValidator(EditText parent) {
this.mParent = parent;
}
public void setAllowEmpty(boolean allowEmpty) {
this.mAllowEmpty = allowEmpty;
}
public void setValidationErrorMsgId(int validationErrorMsgId) {
this.mValidationErrorMsgId = validationErrorMsgId;
}
public void setValidationRules(String strPattern, int validationErrorMsgId, boolean allowEmpty) {
try {
if (!TextUtils.isEmpty(strPattern)) {
mValidationPattern = Pattern.compile(strPattern);
}
} catch (PatternSyntaxException e) {
Log.e(TAG, e.getMessage(), e);
ToastUtil.toastShort("Invalid validation pattern!");
}
this.mValidationErrorMsgId = validationErrorMsgId;
this.mAllowEmpty = allowEmpty;
}
public void setValidLength(int min, int max) {
mMinLength = min;
mMaxLength = max;
}
public void shouldMatch(EditText matchView, int mismatchMsgId) {
this.mMatchView = matchView;
this.mMismatchMsgId = mismatchMsgId;
}
public boolean validate() {
mValidationResult = ValidationResult.Ok;
InputMethodManager imm = (InputMethodManager) mParent.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mParent.getWindowToken(), 0);
final String text = mParent.getText().toString();
final int length = text.length();
if (mValidationResult == ValidationResult.Ok && !mAllowEmpty && 0 == text.length()) {
mValidationResult = ValidationResult.Rules;
}
if (mValidationResult == ValidationResult.Ok) {
if (mMinLength != 0 && length < mMinLength) {
mValidationResult = ValidationResult.Rules;
} else if (mMaxLength != 0 && length > mMaxLength) {
mValidationResult = ValidationResult.Rules;
}
}
if (mValidationResult == ValidationResult.Ok && mValidationPattern != null) {
Matcher m = mValidationPattern.matcher(text);
if (!m.matches())
mValidationResult = ValidationResult.Rules;
}
if (mValidationResult == ValidationResult.Ok && mMatchView != null) {
if (mMatchView.getText().toString().compareTo(text) != 0)
mValidationResult = ValidationResult.Mismatch;
}
if (ValidationResult.Ok == mValidationResult) {
mParent.setError(null);
} else {
CharSequence error = null;
if (ValidationResult.Rules == mValidationResult)
error = MyApplication.getContext().getText(mValidationErrorMsgId);
else if (ValidationResult.Mismatch == mValidationResult)
error = MyApplication.getContext().getText(mMismatchMsgId);
mParent.setError(error);
mParent.requestFocus();
}
return mValidationResult == ValidationResult.Ok;
}
}
Usage:
mSignupEmail = (EditTextWithValidation) root.findViewById(R.id.signup_email);
mSignupEmail.getCustomValidator().setValidationRules(
"[a-zA-Z0-9_-]+(?:\\.[a-zA-Z0-9_-]+)*#[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9_-]+)*\\.(?:[a-zA-Z]{2,})",
R.string.email_answer_validation_msg,
false);
mSignupEmail.getCustomValidator().setValidLength(0, 50);
…
mSignupPassword = (EditTextWithValidation) root.findViewById(R.id.signup_password);
mSignupPassword.getCustomValidator().setValidationRules(
"[a-zA-Z0-9!##$%^&*()]{6,20}",
R.string.password_validation_msg,
false);
…
mSignupConfirmPassword = (EditTextWithValidation) root.findViewById(R.id.signup_confirm_password);
mSignupConfirmPassword.getCustomValidator().setAllowEmpty(true);
mSignupConfirmPassword.getCustomValidator().shouldMatch(mSignupPassword, R.string.password_mismatch);
mSignupConfirmPassword.getCustomValidator().setValidationErrorMsgId(R.string.password_validation_msg);
…
if (mSignupEmail.getCustomValidator().validate() && mSignupPassword.getCustomValidator().validate() && mSignupConfirmPassword.getCustomValidator().validate()) {
// DO SOMETHING
}
Install this app: https://play.google.com/store/apps/details?id=com.desarrollodroide.repos
Go to: Utils -> Android-Validator -> View Demo
There are a bunch of other cool libraries in this app as well. The good thing is that you can view demo of each library and get the link to github repo of that particular library if you like it. It's very useful.
Related
I have a frustrating problem, I have created a custom preference for Android, using the support library.
public class CustomTimePreference extends DialogPreference {
public int hour = 0;
public int minute = 0;
public CustomTimePreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public static int parseHour(String value) {
try {
String[] time = value.split(":");
return (Integer.parseInt(time[0]));
} catch (Exception e) {
return 0;
}
}
public static int parseMinute(String value) {
try {
String[] time = value.split(":");
return (Integer.parseInt(time[1]));
} catch (Exception e) {
return 0;
}
}
public static String timeToString(int h, int m) {
return String.format("%02d", h) + ":" + String.format("%02d", m);
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getString(index);
}
#Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
String value;
if (restoreValue) {
if (defaultValue == null) value = getPersistedString("00:00");
else value = getPersistedString(defaultValue.toString());
} else {
value = defaultValue.toString();
}
hour = parseHour(value);
minute = parseMinute(value);
}
public void persistStringValue(String value) {
persistString(value);
}
}
and
public class CustomTimePreferenceDialogFragmentCompat extends PreferenceDialogFragmentCompat implements DialogPreference.TargetFragment {
TimePicker timePicker = null;
#Override
protected View onCreateDialogView(Context context) {
timePicker = new TimePicker(context);
return (timePicker);
}
#Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
timePicker.setIs24HourView(true);
CustomTimePreference pref = (CustomTimePreference) getPreference();
timePicker.setCurrentHour(pref.hour);
timePicker.setCurrentMinute(pref.minute);
}
#Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
CustomTimePreference pref = (CustomTimePreference) getPreference();
pref.hour = timePicker.getCurrentHour();
pref.minute = timePicker.getCurrentMinute();
String value = CustomTimePreference.timeToString(pref.hour, pref.minute);
if (pref.callChangeListener(value)) pref.persistStringValue(value);
}
}
#Override
public Preference findPreference(CharSequence charSequence) {
return getPreference();
}
}
For completeness, the xml contained within the preferences.xml is:
<customcontrols.CustomTimePreference
android:key="time_pace_preference"
android:title="#string/title_time_pace_preference"
android:defaultValue="#string/settings_default_pace"
android:summary="Set some time"
/>
However, when I attempt to call
PreferenceManager.setDefaultValues(mContext, preferences, true);
I receive
java.lang.ClassCastException: customcontrols.CustomTimePreference cannot be cast to android.preference.Preference
Why is this happening? as CustomTimePreference extends DialogPreference which itself extends Preference, this should be fine?!
If I don't call the setDefaultValues() I am able to go into my settings fragment and view the custom control?
What am I doing wrong, and how do I fix it!?
If you are extending android.support.v7.preference.DialogPreference that will cause this crash.
If so, you can use android.support.v7.preference.PreferenceManager#setDefaultValues(android.content.Context, int, boolean) instead.
I am trying to set the limit of an integer with a minimum value of 0 and maximum of 10 in android with the help of databinding.
For that i have a bindable adapter which set the value of an integer with two listener one increase the value and the other decrease it. Now finally i wanted to set the limit of that integer, minimum of 0 and maximum of 10.
#BindingAdapter("quantity")
public static void setQuantityText(TextView textView, int quantity) {
textView.setText(String.valueOf(quantity));
}
public static class ListenerIncrease implements View.OnClickListener {
private FragmentBinding binding;
public ListenerIncrease(FragmentBinding binding) {
this.binding = binding;
}
#Override
public void onClick(View v) {
int quantity = binding.getQuantity();
binding.setQuantity(++quantity);
}
}
public static class ListenerDecrease implements View.OnClickListener {
private FragmentBinding binding;
public ListenerDecrease(FragmentBinding binding) {
this.binding = binding;
}
#Override
public void onClick(View v) {
int quantity = binding.getQuantity();
binding.setQuantity(--quantity);
}
}
I think it would be easier if you handle the clicks a little differently. The lambda expression part only works in Android Studio 2.1 and above.
<Button android:onClick="#{(view)->Handlers.increment(view, 10)}" .../>
<Button android:onClick="#{(view)->Handlers.decrement(view, 0)}" .../>
<TextView app:quantity="#{quantity}"/>
And then your handler class has:
public static void increment(View view, int max) {
FragmentBinding binding = DataBindingUtil.findBinding(view);
binding.setQuantity(Math.max(max, binding.getQuantity() + 1));
}
public static void decrement(View view, int min) {
FragmentBinding binding = DataBindingUtil.findBinding(view);
binding.setQuantity(Math.min(min, binding.getQuantity() - 1));
}
Alternatively, you can use full-blown two-way binding. In the forthcoming Android Studio 2.2, you'll be able to do this:
<Button android:onClick="#{()->Handlers.increment(quantityView, 10)}" .../>
<Button android:onClick="#{()->Handlers.decrement(quantityView, 0)}" .../>
<TextView android:id="#+id/quantityView" app:quantity="#={`` + quantity}"/>
And then your handler class has:
private static int getCurrentIntValue(TextView view) {
try {
return Integer.parseInt(view.getText().toString());
} catch (NumberFormatException e) {
return 0;
}
}
public static void increment(TextView view, int max) {
int value = getCurrentIntValue(view);
binding.setQuantity(Math.max(max, value + 1));
}
public static void decrement(View view, int min) {
int value = getCurrentIntValue(view);
binding.setQuantity(Math.min(min, value - 1));
}
The trick added in Android Studio 2.2 is the support for the conversions for string concatenation with empty string. It is a useful shortcut. Without that (Android Studio 2.1), you'll need to add your own two-way binding for integer-to-TextView text:
#InverseBindingAdapter(attribute = "quantity")
public static int getQuantity(TextView view) {
return getCurrentIntValue(view);
}
and here's a simplified binding adapter. Use the one in TextViewBindingAdapter.java as a template if you need to add text watchers to the same TextView:
#BindingAdapter("onQuantityChanged")
public static void setQuanityWatcher(TextView view,
final InverseBindingListener quantityChanged) {
TextWatcher newTextWatcher;
if (quantityChanged == null) {
newTextWatcher = null;
} else {
newTextWatcher = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
quantityChanged.onChange();
}
// others are empty...
}
}
TextWatcher oldTextWatcher = ListenerUtil.trackListener(
view, newTextWatcher, R.id.textWatcher);
if (oldTextWatcher != null) {
view.removeTextChangeListener(oldTextWatcher);
}
if (newTextWatcher != null) {
view.addTextChangedListener(newTextWatcher);
}
}
Note that I haven't compiled any of this, so there may be typos.
I've created a custom preference that embeds two buttons (here I have subclassed Button as FastButton). The problem is executing the persistInt to store the preference drastically slows down the response of the button.
I had the notion of only executing the persistInt when the preference's lifecycle is ended, but could not find an appropriate method to override (i.e. there is nothing like onPause() for the Preference class).
I was also unsuccessful at trying to use AsyncTask to move the persistInt off of the UI thread.
Any suggestions about how I should go about mitigating the effect of persistInt on my UI response?
public final class StepperPreference extends Preference {
public int mCurrentValue = 1;
public int maxValue = Integer.MAX_VALUE;
public int minValue = Integer.MIN_VALUE;
private TextView mText;
private FastButton plusButton;
private FastButton minusButton;
public StepperPreference(Context context) {
super(context);
}
public StepperPreference(Context context, AttributeSet attrs) {
super(context, attrs);
parseCustomAttributes(attrs);
}
public StepperPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
parseCustomAttributes(attrs);
}
public void setmCurrentValue(int value) {
if (mCurrentValue != value) {
mCurrentValue = value;
persistInt(mCurrentValue);
}
}
private void parseCustomAttributes(AttributeSet attrs) {
int maxValueAttrInt=Integer.MAX_VALUE;
int minValueAttrInt=Integer.MIN_VALUE;
if (attrs!=null) {
TypedArray a=getContext()
.obtainStyledAttributes(attrs,
R.styleable.StepperPreference,
0, 0);
maxValueAttrInt = a.getInt(R.styleable.StepperPreference_maxValue, Integer.MAX_VALUE);
minValueAttrInt = a.getInt(R.styleable.StepperPreference_minValue, Integer.MIN_VALUE);
a.recycle();
}
if (maxValueAttrInt > minValueAttrInt) {
maxValue = maxValueAttrInt;
minValue = minValueAttrInt;
}
}
#Override
protected View onCreateView(ViewGroup parent) {
LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) li.inflate(R.layout.stepper_preference, parent, false);
mText = (TextView) view.findViewById(R.id.text_view);
Context context = getContext();
int localDefaultValue = 0;
mCurrentValue = getPersistedInt(localDefaultValue);
mText.setText(Integer.toString(mCurrentValue));
plusButton = (FastButton) view.findViewById(R.id.plus_button);
plusButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mCurrentValue < maxValue) {
mCurrentValue++;
mText.setText(Integer.toString(mCurrentValue));
persistInt(mCurrentValue);
}
}
});
minusButton = (FastButton) view.findViewById(R.id.minus_button);
minusButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mCurrentValue > minValue) {
mCurrentValue--;
mText.setText(Integer.toString(mCurrentValue));
persistInt(mCurrentValue);
}
}
});
return view;
}
#Override
protected Object onGetDefaultValue(TypedArray a, int index) {
int localDefaultValue = 0;
Object result = a.getInt(index, localDefaultValue);
return result;
}
#Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
int localDefaultValue = 0;
setmCurrentValue(restoreValue ? this.getPersistedInt(localDefaultValue) : (int) defaultValue);
}
}
CheckBoxPreference, via its TwoStatePreference superclass, uses persistBoolean() for saving the preference value, much as you are using persistInt(). I do not perceive significant latency in the processing of the CheckBox. This means one of two things:
I'm a troglodyte and am incapable of seeing obvious delays in animations and such
CheckBoxPreference does not exhibit the problems that you are seeing in your StepperPreference
note: these two possibilities are not mutually exclusive
If we assume #2 to be correct, then there's something else afoot. Method tracing, to see where you are spending time "downstream" from persistInt(), may prove useful for determining what is different about StepperPreference.
From your comment, you had a listener responding to preference changes, and that was what was causing the sluggish response. "Inline" preferences, like CheckBoxPreference and StepperPreference, will be somewhat more "twitchy" than DialogPreference subclasses like ListPreference, simply because it takes less work to change the preference state (e.g., one screen tap versus 2+). As a result, listeners need to be cheaper. For example, you might hold off on doing significant work until the user has left the PreferenceFragment and so you know that the preference values are likely to be stable for at least a second or so.
The problem wit onSelectedDayChange is that, when i open the calendarview and click on the focused day, nothing happens. For example i want to see details for today. Now i open the calendarview and today is focused. So i have to click on a other day ond click back on today to view the details. Is there any trick do fix this?
CalendarView derives from View which can generate onClick() events. You could try capturing the onClick() event from the view. Just a thought
I made the following class to solve this problem.
public class CustomCalendarView extends CalendarView {
private final String LOG_HEADER = "CAL:VW";
private Date previousSelectedDate = new Date();
private OnDateChangeListener listener;
private CheckSameSelectedDateAsyncTask task = new CheckSameSelectedDateAsyncTask();
public CustomCalendarView(Context context) {
super(context);
}
public CustomCalendarView(Context context, AttributeSet attribute) {
super(context, attribute);
}
public CustomCalendarView(Context context, AttributeSet attribute, int defStyle) {
super(context, attribute, defStyle);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if ((task.getStatus() == AsyncTask.Status.PENDING) || (task.getStatus() == AsyncTask.Status.FINISHED)) {
task = new CheckSameSelectedDateAsyncTask();
task.execute();
}
return false;
}
private void checkSameSelectedDate() {
Date selectedDate = new Date(super.getDate());
if (selectedDate.getDay() == previousSelectedDate.getDay() &&
selectedDate.getMonth() == previousSelectedDate.getMonth() &&
selectedDate.getYear() == previousSelectedDate.getYear()) {
if (listener != null) {
this.listener.onSameSelectedDayChange(this, selectedDate.getYear(), selectedDate.getMonth(), selectedDate.getDay());
}
}
this.previousSelectedDate = selectedDate;
}
public void setSameSelectedDayChangeListener(OnDateChangeListener listener) {
this.listener = listener;
}
public interface OnDateChangeListener extends CalendarView.OnDateChangeListener {
void onSameSelectedDayChange(CalendarView view, int year, int month, int day);
}
private class CheckSameSelectedDateAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
//Note: Breaking point between 75 - 100
Thread.sleep(300);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
checkSameSelectedDate();
}
}
}
In Stackview, it seems that OnItemSelectedListener (from superclass
"AdapterView") is never called...
How can I trigger some event when the view on top of the stack is
changed by the user ?
I want to display some text to show the position of the current item
inside the stack, so I need to find a way to update the textview when the user browses through the stack.
Thanks,
A little late for the party but for folks coming here from google. Fortunately I found an easier solution. It still involves extending StackView though.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.StackView;
public class StackViewAdv extends StackView
{
public StackViewAdv(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public StackViewAdv(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
}
#Override
public void setDisplayedChild(int whichChild)
{
this.getOnItemSelectedListener().onItemSelected(this, null, whichChild, -1);
super.setDisplayedChild(whichChild);
}
}
Please note that this solution only gives the index of the selected view to the listener and view (second parameter on onItemSelected) is null!
Using this.getCurrentView() instead of null unfortunately doesn't work because it returns a sub class of StackView. Maybe someone finds a solution to that.
What i have done is writing a new class extending StackView and writing some code to get the OnItemSelected logics works. When the onTouchEvent gives me a MotionEvent.getAction() == ACTION_UP, i start a Thread that calls himself 'till the StackView.getDisplayedChild() changes. When it changes, i start the OnItemSelected logic, so i can always get the first displayed child.
public boolean onTouchEvent(MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP && this.getAdapter() != null) {
mPreviousSelection = this.getDisplayedChild();
post(mSelectingThread);
}
return super.onTouchEvent(motionEvent);
}
This thread cycles himself untill he gets the new displayedChild:
private class SelectingThread implements Runnable {
CustomStackView mStackView;
public SelectingThread(CustomStackView stackView) {
this.mStackView = stackView;
}
#Override
public void run() {
if(mStackView.getAdapter() != null) {
if (mPreviousSelection == CustomStackView.this.getDisplayedChild()) {
mThisOnItemSelectedListener.onItemSelected(mStackView, mStackView.getAdapter().getView(mPreviousSelection, null, mStackView),
mStackView.mPreviousSelection, mStackView.getAdapter().getItemId(mPreviousSelection));
return;
} else {
mPreviousSelection = mStackView.getDisplayedChild();
mStackView.post(this);
}
}
}
}
This Listener instead sets the Selected flag to true after deselecting them all.
private class StackViewOnItemSelectedListener implements OnItemSelectedListener {
CustomStackView mStackView;
public StackViewOnItemSelectedListener(CustomStackView stackView) {
this.mStackView = stackView;
}
#Override
public void onItemSelected(AdapterView<?> parent, View selectedView, int position, long id) {
deselectAll();
if (mStackView.getAdapter() != null) {
if (mOnItemSelectedListener != null) {
mStackView.mOnItemSelectedListener.onItemSelected(parent, selectedView, position, id);
}
mStackView.getAdapter().getView(position, null, mStackView).setSelected(true);
}
}
private void deselectAll() {
if (mStackView.getAdapter() != null) {
int adapterSize = mStackView.getAdapter().getCount();
for (int i = 0; i < adapterSize; i++) {
mStackView.getAdapter().getView(i, null, mStackView).setSelected(false);
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
if (mStackView.getAdapter() != null) {
if (mOnItemSelectedListener != null) {
mStackView.mOnItemSelectedListener.onNothingSelected(parent);
}
deselectAll();
}
}
}
I've tested it a little and it works..