Skip to content

Commit

Permalink
Add isValid method
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanWooll committed Apr 6, 2015
1 parent 2fb241a commit f57909e
Showing 1 changed file with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
public class FloatingLabelTextView extends FrameLayout implements TextWatcher, View.OnFocusChangeListener {
private EditText mEditText;
private TextView mTextViewHintTop;
private String validationMessage;
private String hintText;
private boolean allowEmpty;
private int validatorType;
private Validator validator;
private boolean isEmpty;
private String mValidationMessage;
private String mHintText;
private boolean mAllowEmpty;
private int mValidatorType;
private Validator mValidator;
private boolean mIsEmpty;
private int mMainTextSize;
private boolean mIsValid;

private int red;
private int green;
Expand All @@ -36,10 +37,10 @@ public FloatingLabelTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.FloatingLabelTextView, 0, 0);
try {
validationMessage = a.getString(R.styleable.FloatingLabelTextView_validationMessage);
hintText = a.getString(R.styleable.FloatingLabelTextView_hint);
allowEmpty = a.getBoolean(R.styleable.FloatingLabelTextView_allowEmpty, true);
validatorType = a.getInt(R.styleable.FloatingLabelTextView_validatorType, -1);
mValidationMessage = a.getString(R.styleable.FloatingLabelTextView_validationMessage);
mHintText = a.getString(R.styleable.FloatingLabelTextView_hint);
mAllowEmpty = a.getBoolean(R.styleable.FloatingLabelTextView_allowEmpty, true);
mValidatorType = a.getInt(R.styleable.FloatingLabelTextView_validatorType, -1);
mMainTextSize = a.getInt(R.styleable.FloatingLabelTextView_textSize, 18);
} finally {
a.recycle();
Expand All @@ -59,14 +60,14 @@ private void init() {
mEditText.setTextSize((float) mMainTextSize);
mEditText.addTextChangedListener(this);
mTextViewHintTop.setTextSize(mMainTextSize * .7f);
mTextViewHintTop.setText(hintText);
mTextViewHintTop.setText(mHintText);
red = getResources().getColor(android.R.color.holo_red_light);
green = getResources().getColor(android.R.color.holo_green_light);
validator = new Validator(allowEmpty, validatorType);
mValidator = new Validator(mAllowEmpty, mValidatorType);

mTextViewHintTop.setY(70);
mEditText.setOnFocusChangeListener(this);
isEmpty = true;
mIsEmpty = true;
}

@Override
Expand All @@ -76,11 +77,11 @@ public void beforeTextChanged(CharSequence s, int start, int count, int after) {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
isEmpty = s.length() < 1;
validationMessage = validator.validate(s);
boolean isValid = TextUtils.isEmpty(validationMessage);
mTextViewHintTop.setTextColor(isValid ? green : red);
mTextViewHintTop.setText(isValid ? hintText + "" : hintText + " - " + validationMessage);
mIsEmpty = s.length() < 1;
mValidationMessage = mValidator.validate(s);
mIsValid = TextUtils.isEmpty(mValidationMessage);
mTextViewHintTop.setTextColor(mIsValid ? green : red);
mTextViewHintTop.setText(mIsValid ? mHintText + "" : mHintText + " - " + mValidationMessage);
}

@Override
Expand All @@ -93,7 +94,7 @@ public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mTextViewHintTop.animate().translationY(0);
} else {
if (isEmpty) {
if (mIsEmpty) {
mTextViewHintTop.animate().translationY(70);
}

Expand All @@ -103,4 +104,8 @@ public void onFocusChange(View v, boolean hasFocus) {
public Editable getText() {
return mEditText.getText();
}

public boolean isValid() {
return mIsValid;
}
}

0 comments on commit f57909e

Please sign in to comment.